943,532 Members | Top Members by Rank

Ad:
Jan 1st, 2009
0

Forms and dropdowns

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

html Syntax (Toggle Plain Text)
  1. <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)
  1. <form method="post" action="page1.php"><input name="title" id="title" type="text" value="" />
  2. <select name="dropdown"><option value="1">Page1<option value="2">Page2<option value="3">Page3<option value="4">Page4</select>
  3. <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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jumbla is offline Offline
14 posts
since Jan 2009
Jan 1st, 2009
0

Re: Forms and dropdowns

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
Reputation Points: 10
Solved Threads: 4
Light Poster
DiGSGRL is offline Offline
45 posts
since May 2008
Jan 2nd, 2009
0

Re: Forms and dropdowns

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)
Last edited by kanaku; Jan 2nd, 2009 at 3:19 am.
Reputation Points: 70
Solved Threads: 15
Posting Whiz
kanaku is offline Offline
378 posts
since Jan 2007
Jan 3rd, 2009
0

Re: Forms and dropdowns

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jumbla is offline Offline
14 posts
since Jan 2009
Jan 3rd, 2009
0

Re: Forms and dropdowns

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).
Reputation Points: 10
Solved Threads: 4
Light Poster
DiGSGRL is offline Offline
45 posts
since May 2008
Jan 5th, 2009
0

Re: Forms and dropdowns

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jumbla is offline Offline
14 posts
since Jan 2009
Jan 7th, 2009
0

Re: Forms and dropdowns

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:
HTML and CSS Syntax (Toggle Plain Text)
  1. $selected = $_POST['dropdown'];
  2. // do some variable cleaning here...
  3.  
  4. if ($selected == 1)
  5. include ("dogs.php");
  6. else if ($selected == 2)
  7. 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.
Reputation Points: 70
Solved Threads: 15
Posting Whiz
kanaku is offline Offline
378 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in HTML and CSS Forum Timeline: CSS/XHTML Layout issue when rendering?
Next Thread in HTML and CSS Forum Timeline: Need Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC