| | |
Forms and dropdowns
Please support our HTML and CSS advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2009
Posts: 14
Reputation:
Solved Threads: 0
Hi guys, I have a simple form that takes a search input and fires it off to page1.php:
I would like to be able to select which page the query is fired off to based on a dropdown menu:
Can you please tell me how to alter that second set of code so that it will go to page2.php or page3.php etc depending on which value is selected from the dropdown?
html Syntax (Toggle Plain Text)
<form method="post" action="page1.php"><input name="title" id="title" type="text" value="" /> <input type="submit" name="search" value="Search" /></form>
I would like to be able to select which page the query is fired off to based on a dropdown menu:
html Syntax (Toggle Plain Text)
<form method="post" action="page1.php"><input name="title" id="title" type="text" value="" /> <select name="dropdown"><option value="1">Page1<option value="2">Page2<option value="3">Page3<option value="4">Page4</select> <input type="submit" name="search" value="Search" /></form>
Can you please tell me how to alter that second set of code so that it will go to page2.php or page3.php etc depending on which value is selected from the dropdown?
Last edited by peter_budo; Jan 6th, 2009 at 9:46 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
I think the best way to do this is to have the form validate to another page which then redirects based on the option that is selected.
So:
<form method="post" action="redirector.php">
On your redirector.php, check what value was sent:
if($_POST['dropdown']==1)
header("Location:page1.php")
and so on.
If you just want to change the action of the form based on the dropdown selection you could do:
action="<?php $_POST['dropdown']?>"
This of course would not redirect the user but it would change where the form was sent after submission.
Hope this helps
So:
<form method="post" action="redirector.php">
On your redirector.php, check what value was sent:
if($_POST['dropdown']==1)
header("Location:page1.php")
and so on.
If you just want to change the action of the form based on the dropdown selection you could do:
action="<?php $_POST['dropdown']?>"
This of course would not redirect the user but it would change where the form was sent after submission.
Hope this helps
A little clarification goes a long way.
I agree with the cat. =) It's better to do this on the server-side.
But if you insist, you can try this:
This will give an action of "1" or "2", etc.. since those are the values of your drop down list.
I set the form action to null initially so that when the user tries to submit without choosing an action, nothing will happen.
However, javascript might not be the best (secure?) way of doing this. (still agreeing with black-cat)
But if you insist, you can try this:
<form method="post" name="dynamicform" action=""><input name="title" id="title" type="text" value="" /> <select name="dropdown" onChange="document.dynamicform.action=(this.value)"><option value="1">Page1<option value="2">Page2<option value="3">Page3<option value="4">Page4</select> <input type="submit" name="search" value="Search" /></form>
This will give an action of "1" or "2", etc.. since those are the values of your drop down list.
I set the form action to null initially so that when the user tries to submit without choosing an action, nothing will happen.
However, javascript might not be the best (secure?) way of doing this. (still agreeing with black-cat)
Last edited by kanaku; Jan 2nd, 2009 at 3:19 am.
If you know ASP, you can save other daniweb members from idiots like me by helping out in this forum.
Visit this thread if your username starts with one of the following letters: B D F H J L N P R T X Y Z.
Visit this thread if your username starts with one of the following letters: B D F H J L N P R T X Y Z.
•
•
Join Date: Jan 2009
Posts: 14
Reputation:
Solved Threads: 0
Thanks for both your replies.
It seems the best way is to do it server side as per both your suggestions.
If I use a redirector.php is :
if($_POST['dropdown']==1)
header("Location:page1.php")
...the only code I need to include in the php file?
Will it still submit the input from the text box as well?
What I am trying to achieve is a form that performs a search via a different php file (page1.php, page2.php etc) depending on which dropdown was selected. eg. searching for the user input on the cats page or dogs page, depending on whether cats or dogs was selected as a dropdown.
The (example) cats.php and dogs.php files are already written, and cannot be combined or anything, hence the reason for a dropdown choice in the first place.
As I'm getting a results page off, I think I need to use the code from your first example DiGSGRL that does redirect the user.
Thank you to you both for your help so far!
It seems the best way is to do it server side as per both your suggestions.
If I use a redirector.php is :
if($_POST['dropdown']==1)
header("Location:page1.php")
...the only code I need to include in the php file?
Will it still submit the input from the text box as well?
What I am trying to achieve is a form that performs a search via a different php file (page1.php, page2.php etc) depending on which dropdown was selected. eg. searching for the user input on the cats page or dogs page, depending on whether cats or dogs was selected as a dropdown.
The (example) cats.php and dogs.php files are already written, and cannot be combined or anything, hence the reason for a dropdown choice in the first place.
As I'm getting a results page off, I think I need to use the code from your first example DiGSGRL that does redirect the user.
Thank you to you both for your help so far!
So, you have a user enter a search term then select which page they are going to search on.
The only thing that the code shown will do is redirect a person depending on the option they select from the dropdown box.
You will need to accept the search term that they enter into the search box either through post or get.
It looks like you already chose POST as the action for your form. So, you would take the post variable $_POST['searchTerm'](searchTerm or something more descriptive would probably be more helpful then title for an ID).
Take your variable, do the necessary cleansing of it(Never trust user input), then use it to search the page(obviously need more code here depending on what you are planning to do to actually search the page).
The only thing that the code shown will do is redirect a person depending on the option they select from the dropdown box.
You will need to accept the search term that they enter into the search box either through post or get.
It looks like you already chose POST as the action for your form. So, you would take the post variable $_POST['searchTerm'](searchTerm or something more descriptive would probably be more helpful then title for an ID).
Take your variable, do the necessary cleansing of it(Never trust user input), then use it to search the page(obviously need more code here depending on what you are planning to do to actually search the page).
A little clarification goes a long way.
•
•
Join Date: Jan 2009
Posts: 14
Reputation:
Solved Threads: 0
You're confusing me a little bit, but I suspect I haven't been clear with my original question 
The pages I am firing my user input onto are already written, so all I need to do is pass my search input to those pages.
EXAMPLE:
A user enters the name of an animal, and then chooses the dropdown "dogs" cats" or "rabbits". Depending on which is chosen, the user input is POSTed to either dogs.php, cats.php or rabbits.php.
Each of those php files is already coded to search the relevant databases from the site I am getting them from (this is probably the bit that sounds weird).
All I need is a way to make sure that you can choose which php page gets the search input fired off to.

The pages I am firing my user input onto are already written, so all I need to do is pass my search input to those pages.
EXAMPLE:
A user enters the name of an animal, and then chooses the dropdown "dogs" cats" or "rabbits". Depending on which is chosen, the user input is POSTed to either dogs.php, cats.php or rabbits.php.
Each of those php files is already coded to search the relevant databases from the site I am getting them from (this is probably the bit that sounds weird).
All I need is a way to make sure that you can choose which php page gets the search input fired off to.
We understand what you're trying to do. The cats.php and dogs.php ARE server-side BUT the javascript for checking the value of the selected option and changing the form action is NOT.
So instead of using the javascript code I gave you earlier, the others are suggesting you have one file (ie) processform.php.
Inside processform.php is a simple script that goes like this:
Of course the others suggested a redirect, but I don't want to be sued for plagiarizing. They should work more or less the same way.
So instead of using the javascript code I gave you earlier, the others are suggesting you have one file (ie) processform.php.
Inside processform.php is a simple script that goes like this:
HTML and CSS Syntax (Toggle Plain Text)
$selected = $_POST['dropdown']; // do some variable cleaning here... if ($selected == 1) include ("dogs.php"); else if ($selected == 2) include ("cats.php");
Of course the others suggested a redirect, but I don't want to be sued for plagiarizing. They should work more or less the same way.
If you know ASP, you can save other daniweb members from idiots like me by helping out in this forum.
Visit this thread if your username starts with one of the following letters: B D F H J L N P R T X Y Z.
Visit this thread if your username starts with one of the following letters: B D F H J L N P R T X Y Z.
![]() |
Similar Threads
- Web Developed in Dreamweaver Help! (HTML and CSS)
- The most famous CSS web sites galleries in the world (HTML and CSS)
- for validation - text box and drop down menu (JavaScript / DHTML / AJAX)
Other Threads in the HTML and CSS Forum
- Previous Thread: CSS/XHTML Layout issue when rendering?
- Next Thread: Need Help
| Thread Tools | Search this Thread |
appointments asp background backgroundcolor beta browser bug calendar cart cgi code codeinjection corporateidentity css design development displayimageinsteadofflash dreamweaver emailmarketing epilepsy explorer firefox flash form format google griefers hackers hitcounter hover html ide ie7 ie8 iframe image images internet internetexplorer intranet iphone javascript jpeg layout macbook maps marketshare microsoft mozilla multimedia navigationbars news offshoreoutsourcingcompany opacity opera optimization pnginie6 positioning problem scroll seo shopping studio swf swf. textcolor timecolor titletags url urlseparatedwords visual visualization web webdevelopment webform website windows7





