| View previous topic :: View next topic |
hex4
Joined: 13 Nov 2007 Posts: 21 Location: Qormi, Malta
|
Posted: Fri Nov 16, 2007 7:58 pm Post subject: Getting value of <option> in <select> tag |
|
|
|
Let's say I have a select statement as follow:
| Code: |
<select name="testing">
<option value="1">Test1</option>
<option value="2">Test2</option>
<option value="3">Test3</option>
<option value="4">Test4</option>
</select>
|
and this select tag is in a form using method="POST".
Is it possible to get the text of the option selected when submitting the form?
I know that if I do $_POST['testing'] I will get back 1,2,3 or 4 but I would like to get Test1, Test2, Test3 or Test4 instead.
Regards |
|
| Back to top |
|
|
yancho Site Admin
Joined: 13 Nov 2007 Posts: 56 Location: Iklin
|
Posted: Fri Nov 16, 2007 8:34 pm Post subject: |
|
|
|
replace the numbers with the actual value .. anything besides that is unknown to me sorry
| Code: |
<option value="Test 1">Test1</option>
|
_________________
 |
|
| Back to top |
|
|
hex4
Joined: 13 Nov 2007 Posts: 21 Location: Qormi, Malta
|
Posted: Fri Nov 16, 2007 8:39 pm Post subject: |
|
|
|
| yancho wrote: | replace the numbers with the actual value .. anything besides that is unknown to me sorry
| Code: |
<option value="Test 1">Test1</option>
|
|
And in my case, that is not possible since the value of the options will be used for an SQL query.
Ex:
ID: 1 Name: Josef
ID: 2 Name: Nicholas
ID: 3 Name: Pauline
if 1 is selected, then update that row and once updated say: Student Josef was updated successfully.
I know i have the option to create another SELECT statement to get the name again but I would like to avoid that if possible |
|
| Back to top |
|
|
yancho Site Admin
Joined: 13 Nov 2007 Posts: 56 Location: Iklin
|
Posted: Fri Nov 16, 2007 11:03 pm Post subject: |
|
|
|
You have to load the names so u can select them no?
so what you can do is
| Code: |
<option value="<?echo $row['student_id']; ?>"><?echo $row['student_name'];</option>
|
.. Then
| Code: |
<?
echo " $_POST['selected_option'] was updated successfuly; "
?>
|
Problem solved  _________________

Last edited by yancho on Sat Nov 17, 2007 1:35 am; edited 1 time in total |
|
| Back to top |
|
|
hex4
Joined: 13 Nov 2007 Posts: 21 Location: Qormi, Malta
|
Posted: Fri Nov 16, 2007 11:36 pm Post subject: |
|
|
|
| like that, if I have an SQL statement I can only update by student_name and hence, any duplicate names will be updated too... |
|
| Back to top |
|
|
yancho Site Admin
Joined: 13 Nov 2007 Posts: 56 Location: Iklin
|
Posted: Sat Nov 17, 2007 1:11 am Post subject: |
|
|
|
then load the row_id and update sql where row_id and name match  _________________
 |
|
| Back to top |
|
|
killfr0g
Joined: 13 Nov 2007 Posts: 4 Location: Malta
|
Posted: Sat Nov 17, 2007 7:14 pm Post subject: |
|
|
|
Many approaches to take here..
You could store the loaded id/name pairs in the session as an associative array then access the one you need when you want to display the name but you only have the id.
Alternatively, you can add a prefix to the values in the select.. so your values will be for example "test" + the id (test1, test2 etc. When you get the values in PHP extract the id with a simple function like substr, while using the whole value for the name.
Yet another option is to use some JavaScript.. place a hidden field in the form, and when the user clicks the button to save/edit etc, extract the text of the selected value and place it in the hidden field. Be careful though cos users may disable JavaScript on the browser.. |
|
| Back to top |
|
|
varactor
Joined: 22 Dec 2008 Posts: 10 Location: Malta
|
Posted: Mon Dec 22, 2008 2:08 pm Post subject: |
|
|
|
Relax guys..
I got an easy solution for this..
Make the HTML as follows, separating the values in value="" with a comma.
| Code: | <select name="testing">
<option value="1,Test1">Test1</option>
<option value="2,Test2">Test2</option>
<option value="3,Test3">Test3</option>
<option value="4,Test4">Test4</option>
</select> |
Like this you can use the php function explode as follows
| Code: | $data_raw = explode(",",$testing);
$ID = $data_raw[00];
$Name = $data_raw[01]; |
The text of the <option> can be anything, most important is to make the data you want seprated by commas in the value=""..
thats all!! |
|
| Back to top |
|
|
hex4
Joined: 13 Nov 2007 Posts: 21 Location: Qormi, Malta
|
Posted: Tue Dec 23, 2008 10:23 am Post subject: |
|
|
|
| varactor wrote: | Relax guys..
I got an easy solution for this..
Make the HTML as follows, separating the values in value="" with a comma.
| Code: | <select name="testing">
<option value="1,Test1">Test1</option>
<option value="2,Test2">Test2</option>
<option value="3,Test3">Test3</option>
<option value="4,Test4">Test4</option>
</select> |
Like this you can use the php function explode as follows
| Code: | $data_raw = explode(",",$testing);
$ID = $data_raw[00];
$Name = $data_raw[01]; |
The text of the <option> can be anything, most important is to make the data you want seprated by commas in the value=""..
thats all!! |
you're a bit late for the solution :p
still, thanks anyway. Been using this method for long time now hehe _________________ orlandev.com |
|
| Back to top |
|
|
varactor
Joined: 22 Dec 2008 Posts: 10 Location: Malta
|
Posted: Tue Dec 23, 2008 10:32 am Post subject: |
|
|
|
lol no worries
when an issue is solved, I would not mind posting and seeing posts with alternative solutions, there is always room to learn something new  |
|
| Back to top |
|
|