Getting value of <option> in <select> tag

 
Post new topic   Reply to topic    PHP User Group Malta Forum Index // PHP Projects
View previous topic :: View next topic  
Author Message
hex4



Joined: 13 Nov 2007
Posts: 17
Location: Qormi, Malta

PostPosted: Fri Nov 16, 2007 7:58 pm    Post subject: Getting value of <option> in <select> tag Reply with quote
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
View user's profile Send private message Visit poster's website MSN Messenger
yancho
Site Admin


Joined: 13 Nov 2007
Posts: 51
Location: Iklin

PostPosted: Fri Nov 16, 2007 8:34 pm    Post subject: Reply with quote
replace the numbers with the actual value .. anything besides that is unknown to me Sad sorry

Code:

<option value="Test 1">Test1</option>

_________________
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
hex4



Joined: 13 Nov 2007
Posts: 17
Location: Qormi, Malta

PostPosted: Fri Nov 16, 2007 8:39 pm    Post subject: Reply with quote
yancho wrote:
replace the numbers with the actual value .. anything besides that is unknown to me Sad 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
View user's profile Send private message Visit poster's website MSN Messenger
yancho
Site Admin


Joined: 13 Nov 2007
Posts: 51
Location: Iklin

PostPosted: Fri Nov 16, 2007 11:03 pm    Post subject: Reply with quote
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 Smile
_________________


Last edited by yancho on Sat Nov 17, 2007 1:35 am; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
hex4



Joined: 13 Nov 2007
Posts: 17
Location: Qormi, Malta

PostPosted: Fri Nov 16, 2007 11:36 pm    Post subject: Reply with quote
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
View user's profile Send private message Visit poster's website MSN Messenger
yancho
Site Admin


Joined: 13 Nov 2007
Posts: 51
Location: Iklin

PostPosted: Sat Nov 17, 2007 1:11 am    Post subject: Reply with quote
then load the row_id and update sql where row_id and name match Smile
_________________
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
killfr0g



Joined: 13 Nov 2007
Posts: 4
Location: Malta

PostPosted: Sat Nov 17, 2007 7:14 pm    Post subject: Reply with quote
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
View user's profile Send private message
Post new topic   Reply to topic    PHP User Group Malta Forum Index // PHP Projects All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

alexisRed v1.2 // Theme Created By: Andrew Charron and Web Hosting Bluebook // Icons in Part By: Travis Carden
Boards optimised using the phpBB-SEO mod found at phpbb-seo.com
Boards hosted courtesy of solutions-lab.net
Link Backs : PHPClasses.org - MT Page :: PHPUsergroups.org - MT Page



Powered by phpBB © 2001, 2002 phpBB Group