Pass a html select option to a PHP variable

How can I pass a chosen option from an html select form into a php var

Page 1 of 1

2 Replies - 28848 Views - Last Post: 02 March 2009 - 05:28 AM Rate Topic: -----

#1 thiagomancin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-March 09

Pass a html select option to a PHP variable

Posted 02 March 2009 - 04:03 AM

How can I pass a chosen option from an html select form into a php variable?
I need to equal in an if condition form the below:
If the chosen value is United Kingdom $var = 10
If the chosen value is Europe $var = 20
If the chosen value is Rest of the world $var = 30

<td>Region:</td>
<td colspan="3"><select name="country">
<option value="United Kingdom">United Kingdom</option>
<option value="Europe">Europe</option>
<option value="Rest of the world">Rest of the world</option>
</select>
</td>
Tks alot!!!!

This post has been edited by thiagomancin: 02 March 2009 - 04:06 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Pass a html select option to a PHP variable

#2 rashmisharma  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-February 09

Re: Pass a html select option to a PHP variable

Posted 02 March 2009 - 04:16 AM

Just do the following
$country=$_POST['country'];


Was This Post Helpful? 0
  • +
  • -

#3 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Pass a html select option to a PHP variable

Posted 02 March 2009 - 05:28 AM

So you want the options in the select menu to appear as words, but use numbers for the values? That's fairly easy:

<td>Region:</td>
<td colspan="3"><select name="country">
<option value="10">United Kingdom</option>
<option value="20">Europe</option>
<option value="30">Rest of the world</option>
</select>
</td>



I presume this is already part of a form, so I'm not going to write that bit out for you. You'll need to check the method attribute that you've set on your form tag. If it's set to 'post', then on your php page all the form elements will be available as part of the $_POST array. Surprisingly, if your form method is 'get' then the elements will be in the $_GET array. As rashmisharma quite correctly says, you access the values set in your form by looking in either the $_POST or $_GET collections.

//form method set to get:
$country = $_GET['country'];

//form method set to post:
$country = $_POST['country'];


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1