0

I want form to post automatically if zip variable is passed from URL.

URL looks like: www.sitename.com/maps/zipsearch.php?zip=90210

Form looks like:

<form method="post">
    Zipcode:
    <input name="zip" value="<?php echo (isset($_GET["zip"]))? $_GET["zip"]:"";?>" />

    <input type="submit" name="subbut" value="Find instructors" />
</form>

So it fills the input box with zip code but I would like it to post automatically to see results again if zip is passed.

Maybe an IF / THEN?

Any help would be appreciated.

4 Answers 4

0

You mean to echo the value passed in GET parameter?

<input type="submit" name="subbut" value="<?php echo isset($_GET['zip'])?$_GET['zip']:'Find'; ?>" />

EDIT

Or, if you are asking about submitting the form, then something like this might work I believe:

<input type="submit" name="subbut" value="<?php echo isset($_GET['zip'])?$_GET['zip']:'Find'; ?>" />

<?php if( isset( $_GET['zip'] ) ) { ?>
<script>
  document.forms["name_of_the_form_here"].submit();
</script>
<?php } ?>
Sign up to request clarification or add additional context in comments.

Comments

0

like this:

<form id="form" action="form.php" method="post">
Zipcode:
    <input name="zip" value="<?php echo (isset($_GET["zip"]))? $_GET["zip"]:"";?>" />       

    <input type="submit" name="subbut" value="Find instructors" />
</form>
<?php if (isset($_GET["zip"])): ?>
<script>document.getElementById('form').submit()</script>
<?php endif; ?>

Comments

0

since passing data via URL means GET method, so i think you have a little misconception with your question.

if you would like to post automatically you dont need to show form.

just put this code in your zipsearch.php

if ($_GET['zip'] != ""){
// do what you want if zip parameter is not null
}else{
// do what you want if zip parameter is null
}

Comments

0

It looks like your form is submitting to itself. (Eg. zipsearch.php displays HTML form. When user submits form, it is posted back to zipsearch.php which displays the search results).

If this is the case, you don't have to post anything, because you are already inside the file that handles the form submission. You could do something like this:

<?php

  if (isset ($_POST['zip'])) {
    $zip = $_POST['zip'];             /* Form was submitted */
  } else if (isset ($_GET['zip'])) {
    $zip = $_GET['zip'];              /* "?zip=" parameter exists */
  }

  if (isset ($zip)) {
    /* Display search results */
  } else {
    /* Display form */
  }

Comments

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.