Two Laptop Bag*
The moose likes PHP and the fly likes How to get  selected option with select tag? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Languages » PHP
Bookmark "How to get  selected option with select tag?" Watch "How to get  selected option with select tag?" New topic
Author

How to get selected option with select tag?

Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 222

I have three options depending on , i want to redirect the user on three diferrent pages like
php1.php
php2.php
php3.php

<select>
<option value="admin">Administrator</option>
<option value="perm">Permanent</option>
<option value="guest">Guest</option>
</select>

How to do that.

Oracle Java Programmer , Oracle SQL Expert
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3013
    
  35

Client side:
1. How do you send the selected value to the server? Is there <form> tag or your Javascript does it?
2. How do you identify the name of the parameter? Does your <select> tag has a 'name' attribute or your Javsascript does it?

Server side:
3. Did you try reading the selected value via $_POST?
4. If you did the step 3, did you try using if conditions or switch conditions to check the selected value?
5. If you did all these steps, what all you need is to set a header "Location" header to the redirect path as in header('Location: php1.php');


Founder of ExamLab and Systemup
See how I can help you to become an awesome programmer
Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 222

First of all , i will like to say thank because of your Exam lab i scored 93% in OCJP6 exam.




and the tour.php file


it gives notice undefined constant 'Ajeet'
notice undefined constant 'Mehtab'
notice undefined constant 'Manish'
notice undefined constant 'jitu'
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3013
    
  35

Glad that ExamLab helped

About the question, are you sure that you have used quotes to denote all String literals as in 'Ajeet' and you still get this notice? You get that notice when quotes are not used.

Another thing I noticed is you omitted the 'value' attribute from your <option> tags, so no value will be assigned to your 'hero' parameter (this is where isset is usefil). I can't however understand how you get an undefined constant warning even with that.
Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 222

I want to insert the selected option in the database , how can i do that.

How to get the selected value and make a query to insert in the table in database.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3013
    
  35

Mahtab Alam wrote:How to get the selected value and make a query to insert in the table in database.

Did you manage to obtain the parameter value anyway? If not, show us your code. We can discuss it over here as that's what this topic is all about. If your question is about setting up database connections, or persisting data over a connection, please start a new topic in this form. When you do that, please IsolateTheProblem - you got the param value already, so the question should probably be only on persisting a value to the database.
Mahtab Alam
Ranch Hand

Joined: Mar 28, 2012
Posts: 222

Thank you for your time , i resolved my problem.
nancy jack
Greenhorn

Joined: Jun 12, 2014
Posts: 1
Thanks for sharing this php code, Its really useful for me..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get selected option with select tag?
 
Similar Threads
How to set Default selection in h:selectOneMenu
jsp that posts to itself
inserting JCheckbox inside JTable and rendering
javascript for popup on selected item in dropdown
modify this code to require select instead of a checkbox