0

I have been searching for a few days on how to do this and I have yet to find an example of what I am looking for. Am I looking for the wrong thing?

Here is what I am trying to accomplish using the following code, I am reading in my error code and creating a link out of the error code.

<p><h4>Errors Returned (if any): </h4> 
    <a href="https://www.mysite.com/test/search_form.php?query=<?php echo $ERRORCODE; ?>">
        <?php echo $ERRORCODE; ?>
    </a>
</p>

I am then posting this information to my search form (below):

<?php
if (isset($_GET['query']))
{
    echo "javascript... and such.";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Search - Test Site 2013</title>
  </head>
    <table border="0" cellpadding="10">
      <tr>
        <td>
          <img src="images/test.png">
        </td>
        <td>
          <h1>Search - Test Site 2013</h1>
        </td>
      </tr>
    </table>
<body>
    <form action="search.php" method="GET" name="errorsearch">
        Search by Error Code
        <input type="text" name="query" />
        <input type="submit" value="Search" name="search"/>
    </form>
</body>
</html>

I am using (isset($_GET['query'])) to check if the error code has been passed over in the URL. I am able to successfully display a message or something silly, but I would like to submit the form and check for the error code text BUT ONLY IF query has been set.

It is my understanding that Javascript can do this but I am unsure exactly what is needed. I was researching Javascript submit buttons and on-click events but I think this is the opposite of what I want. I just need JS to initiate the button click.

Can someone point me in the right direction?

4
  • 4
    don't echo anything before your <!DOCTYPE>. If there is any code before the doctype, it will cause IE to throw the page into quirks mode and completely break your layout. Commented Jan 22, 2013 at 15:55
  • Thank you for the tip SDC. I was not aware of that. :) Commented Jan 22, 2013 at 15:57
  • So you want JS to in a sense "click" the button for you? If I understand correctly you click a link which goes to a search page. If the page detects an error code being passed via $_GET display a message? Commented Jan 22, 2013 at 16:02
  • Yes Shawn, that is correct. I am just fuzzy on how to initiate the 'click'. I am very new to JS! Commented Jan 22, 2013 at 16:06

3 Answers 3

1

There are a number of different ways you could do this, but this question has already been answered elsewhere on this site. The answers here should help you: JavaScript code to stop form submission

Sign up to request clarification or add additional context in comments.

Comments

1

If you need JS to initiate button click then just call function in javascript which is called on button click -> onClick listener.

You can create JS boolean variable to see if query was sent for example

var wasQuerySent = ""

and then you can test this if wasQuerySent has certain value.

Comments

0

Why Javascript at all? If I'm understanding correctly, you can handle all of this with PHP:

<?php
// Default to disabled state for submit button
$disabled = 'disabled="disabled"';
if (isset($_GET['query']))
{
    //echo "javascript... and such.";
    // if you get the $_GET['query'] then clear the submit's
    // disabled state
    $disabled = '';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>Search - Test Site 2013</title>
</head>
    <table border="0" cellpadding="10">
    <tr>
        <td>
        <img src="images/test.png">
        </td>
        <td>
        <h1>Search - Test Site 2013</h1>
        </td>
    </tr>
    </table>
<body>
    <form action="search.php" method="GET" name="errorsearch">
        Search by Error Code
        <input type="text" name="query" />
        <input type="submit" value="Search" name="search" <?php echo $disabled; ?>/>
    </form>
</body>
</html>

You could also do this by binding to the form's submit event - in jQuery it's

jQuery('#myForm').on('submit', ...)

3 Comments

I think I misunderstood your question - in the comments it sounds like you want to submit the form automatically if the $_GET['query'] is set?
Yes. If an error code is present in my database, we are pulling that and displaying the error code as a link. When that link is clicked... I would like the error code value posted to my error code search page. However, I feel it is an extra step for the user to have to click submit. I figured I could do it pragmatically.
You can! Have the link the user clicks be something like <a href="/search.php?query=<?php echo $query; ?>">Search Error</a> - would that work? No reason the link has to even go near the page with form if you just want to fire off the error to your search.php page. Of course, you probably want to use PHP to make the error message URL-friendly (see urlencode and urldecode (us3.php.net/manual/en/function.urlencode.php))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.