tags:

views:

70

answers:

6

In my application am submiting my form by using post for a php page.

<form method="post">
<select name="txtplace">
<option value="1">ajith</option>
</select>
</form>

here when i am try to get the value of my dropdown its only getting 1.How can i get ajith

+2  A: 
<option value="ajith">ajith</option>

Should work. You get the value as 1 because you have set the value as 1.

Shoban
thanks for your suggection.but i need both for my application
Ajith
A: 

You'll only get '1' in your form script, as that's the value of the option element. You could change to the following:

<form method="post">
<select name="txtplace">
<option value="ajith">ajith</option>
</select>
</form>

rather than using the ID (I presume) of that item.

richsage
A: 
<option value="ajith">ajith</option> 
Getz
A: 

If you just want "ajith" then do as Shoban suggests. If you need both 1 and "ajith" you could always have: <option value="1-ajith">ajith</option>

And then with php:

$sTxtPlace = $_POST['txtplace'];
list($number,$text) = explode("-",$sTxtPlace,2);
Septih
thanks.actually i need both
Ajith
Check the edit I just made. Use explode not split.
Septih
What if the label itself contains a "-"?
pinaki
the 3rd argument of explode is the limit, meaning in this case it will return only 2 segments, split by the first occurence of "-" so everything after the first "-" is put into $text
Septih
A: 

You can use a hidden element and add an onchange handler to the select to populate the label part. Advantages are that you dont have to change your rendering code and you dont need to explode.

Try:

<form method="post">
<input type="hidden" id="txtPlaceLabel" name="txtPlaceLabel" value="">
<select name="txtplace" onchange="document.getElementById('txtPlaceLabel').value=this.options[this.options.selectedIndex].innerHTML;">
<option value="ajith">ajith</option>
</select>
</form>

Note: Haven't tested the code so maybe some editing will be required.

pinaki
+1  A: 

Maybe it's better if you define an array in php:

$arr = array ( 1 => "ajith" );

Then generate the HTML dynamically:

<option name="test" value="$i">$arr[$i]</option>

Then after post:

$key = $_GET['test'];
$value = $arr[$key];
douwe
This would also be more safe than blindly accept and use the posted value.
Alex