Hello, this is hopefully a very easy one for you guys, but as a beginner I'm not sure how to do this correctly:

I have a form

<form method="post" name="boxsearchform" action="search.php"><input name="title" id=
"title" type="text" value=""  /><input type="image"
name="search" src="go.gif" /></form>

that passes the variable of "title" to search.php. On search.php I use $myvar = $_POST['title']; to get that variable, and use it further down the page.

How can I add a dropdown box to my first page so that instead of searching by the variable title, I can search by the variable colour?

The user would be able to select from a dropdown of either colour or title before pressing the submit button, and the php would capture the appropriate variable on the search.php page.

Recommended Answers

All 7 Replies

easy ;) here you go:

<form method="post" name="boxsearchform" action="search.php">
<label>Choose Something to search:
<select name="choosevar">
  <option value="Title">Title</option>
  <option value="Colour">Colour</option>
</select>
name="search" src="go.gif" />
</form>

Thanks for your post jakx12, but there is now no input box in that code!

I wish for whatever the user types into the input box to be given the ID of either title or colour based on what they then select from the drop down box.

oh sorry, ok, so say for example if they type colour, then you want the search result page to pick this up?

Please explain exactly what you want this form to do and the idea about what input types you would like.

At the minute, my textbox has the ID of title, which is passed to search.php, where it uses $POST to get the vale of what was typed into that box. What I want is for the user to be able to type their input into the text box, and then select from a dropdown list. If they select title from the dropdown list, then the input is sent with the id title. if they select colour, then the input is sent with the id colour.

I can then use both $myvar = $_POST and $myvar = $_POST on search.php to capture whichever input has been sent.

ok i suggest having search.php to have something like this:

<?PHP
if($_POST['choosevar'] == Title ) {
$myvar = $_POST['INPUT'];
}
if($_POST['choosevar'] == Colour ) {
$myvar = $_POST['INPUT'];
}
?>

Then the html will look like this

<form method="post" name="boxsearchform" action="search.php">
<label>Choose Something to search:
<select name="choosevar">
  <option value="Title">Title</option>
  <option value="Colour">Colour</option>
</select>
<input name="title" id=
"INPUT" type="text" value=""  />
name="search" src="go.gif" />
</form>

is this what you are looking for?

I think it just might be. Had to change the name of the input box to INPUT as well as the id, and after that it worked. Have been tearing my hair out all evening! Cheers.

no problem :)

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.