Jump to content

Get values from form


md7dani

Recommended Posts

I'm using the Get Method to create a URL. what I need is to get the value from a select list and put it into a Mysql query. How can I do this?

 

Ex code:

<form target="_self" method="Get">

<select name="city_menu">

    <option value="New York">New York</option>

    <option value="Paris">Paris</option>

    <option value="London">London</option>

  </select>

<input type="submit" name="Posted" value="Select" />

</form>

Can I get the value from the select list in the same page? Outside form or inside doesn't matter.

 

 

Link to comment
https://forums.phpfreaks.com/topic/184720-get-values-from-form/
Share on other sites

<?php

if ( isset($_GET["Posted"]) )
{
    $city = $_GET["city_menu"]; // This would display the value of whichever item was selected from the drop-down, and null if no item was selected.
    $query = "SELECT * FROM TABLE_NAME WHERE city_name='$city'";
    // ...perform mysql query...
}
?>

Link to comment
https://forums.phpfreaks.com/topic/184720-get-values-from-form/#findComment-975167
Share on other sites

It doesn't matter weather you use the get or post method they both will get passed. The difference is the get method the values your passing display in the url. If you use the post method they get passed you just dont see them in the URL. Now with that being said just to cover my bases not sure if you knew that or not.

 

Before I get into how to get the values since your calling the same page I would suggest using a conditional statement if the submit button was pressed process the code or if its loading for the first time it skips the processing code and displays your form. I usually use a switch statement

 

Something like this

<?php
$req = (!isset($_REQUEST['req'])) ? 'default' : $_REQUEST['req'];
switch($req){
case "submit":
     // Get Your Values and store it into variable
    $var = $_GET['city_menu'];

    // Then run sql to add to your database........
default:
?> // Close PHP Tags to write HTML

<form target="_self" method="Get">
<select name="city_menu">
    <option value="New York">New York</option>
    <option value="Paris">Paris</option>
    <option value="London">London</option>
  </select>
[b]<input type="hidden" name="req" value="submit" />
NOTE: This was added to your code to be passed[/b]
<input type="submit" name="Posted" value="Select" />
</form>

<?php
break;
} // End Switch
?>

 

First off I added the hidden field req in your form in bold that will decide weather to run the process code or display the form via your switch statements. Now to get the value that was selected when you hit the submit button you its accessed by the $_GET['city_menu'] if using the get method or $_POST['city_menu'] if you used post method.

 

I stored it in a variable cause personally typing a variable name is easier to me then typing out $_GET or $_POST method.

 

Hope this helps and makes sense if you have any problems let me know

Link to comment
https://forums.phpfreaks.com/topic/184720-get-values-from-form/#findComment-975170
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.