Hi guys, I have a simple form that takes a search input and fires it off to page1.php:

<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:

<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?

Recommended Answers

All 6 Replies

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==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?>"
This of course would not redirect the user but it would change where the form was sent after submission.

Hope this helps

I agree with the cat. =) It's better to do this on the server-side.

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)

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==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 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).

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.

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:

$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. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.