EnergyLynx EnergyLynx
EnergyLynx EnergyLynx

Reputation: 115

Pass Data From Input Form on One Page To a Select Form on Another page?

There are so many examples of passing data from one page to another using GET and POST in PHP.

However what I am trying to do is a bit different.

Here is what i need to do:

I have an input textfield on one page (page1.php) and I need to pass its value/data to a Select form on another page (page2.php).

I have this on page1.php:

<form method="post" action="../page2.php">
  <input type="text"/>
  <button name="location" type="submit">Search</button>
</form>

And I have this on page2.php:

<form id="myForm" name="myForm" class="myForm" method="post" action="page2.php">
  <select style="font-size:9px;" name="timezone2" id="timezone2">

<option value="<?php echo $_GET["location"]; ?>"> </option>

</select>
</form>

when i run the page1.php from a browser, it will point me to the page2.php as it should but it will not show the Data/Value that was input in the textfield name="location" !

First off, is what I am trying to do possible?

and if so, could someone please point me in the right direction please?

Upvotes: 0

Views: 6976

Answers (1)

user1477388
user1477388

Reputation: 21440

You can pass variables via GET and POST. To pass variables to a page via GET, call the page like so:

/myPage.php?myParam=1

Then, you could run the following PHP:

<?php echo $_GET['myParam']; ?>

Which would output "1."

To pass variables via POST, post your data to the page like so:

<form action="myPage.php" method="post">
     <input type="text" name="myInput" />
     <input type="submit" value="Submit">
</form>

Then, on myPage.php you could run the following:

<?php echo $_POST['myInput']; ?>

Which would output whatever was contained in the textbox before you clicked submit.

Edit:

It looks like you are trying to submit data from one page and have that appear in the next page as an option in your select menu. Try this on page1.php:

<form method="post" action="../page2.php">
  <input type="text" name="location" value="THIS WILL APPEAR IN THE SELECT MENU AS AN OPTION"/>
  <button type="submit">Search</button>
</form>

Try this on page2.php:

<form id="myForm" name="myForm" class="myForm" method="post" action="page2.php">
  <select style="font-size:9px;" name="timezone2" id="timezone2">

<option value="<?php echo $_POST["location"]; ?>"><?php echo $_POST["location"]; ?></option>

</select>
</form>

Upvotes: 1

Related Questions