Christian Hernandez
Christian Hernandez

Reputation: 1

How to Automatically POST Form if Variable is passed via URL

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.

Upvotes: 0

Views: 952

Answers (4)

Nisse Engstr&#246;m
Nisse Engstr&#246;m

Reputation: 4751

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 */
  }

Upvotes: 0

Dark Cyber
Dark Cyber

Reputation: 2231

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
}

Upvotes: 0

Kiyan
Kiyan

Reputation: 2203

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; ?>

Upvotes: 0

Akhilesh B Chandran
Akhilesh B Chandran

Reputation: 6608

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 } ?>

Upvotes: 0

Related Questions