Sam Sam - 1 year ago 56
HTML Question

Get option value and append to search as additional keyword?

I have selection fields and a search box. I want users to select a recipe, then search ingredients by keyword in the search box. The search result domain should look like:

The code I created below doesn't seem to append the RECIPE value though, only the keyword.

<select id="recipe-select" class="form-style" name="recipeType">
<option selected disabled>Select Your Recipe</option>
<option value="Cakes" name="selectedRecipe">I want to make Cakes</option>
<option value="Pies" name="selectedRecipe">I want to make Pies</option>
<div class="widget box" id="search-box">
<div class="widget-search">
<form action="" method="post" onSubmit="search();return false;">
<input name="s" class="form-style" type="text" placeholder="Search" id="searchbox">
<button><i class="fa fa-search"></i></button>

<script type="text/javascript">
function search() {
alert("View all results?");
var a = document.getElementById("searchbox").value;
a = "<?php echo $_POST['selectedRecipe']; ?>+" + a;
a = a.replace(" ","+");
document.location = a;

I want the result of the search URL to look like this:

Where the option value is automatically appended as a search keyword. The below doesn't seem to work.

<?php echo $_POST['selectedRecipe']; ?>

Thank you everyone for your input! Resolved! :)

Answer Source

Just add recipe and keywords like this in your search function...

var keyword = document.getElementById("searchbox").value;
var recipe = document.getElementById("recipe-select").value;
a = "" + recipe + ' ' + keyword;

Code should look something like this...

Hope that helps ;)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download