This is my html for the search bar and dropdown.
<form action='seniorProjectLanding.php' method='POST' class='form-group'>
<div class="form-horizontal" align="center">
<input type="text" name="searchResults"
class="form-control"
placeholder="Search"
style="width:80%; max-width:80%; display:inline-block"/>
<button type="submit"
class="btn btn-primary"
name="submit"
style="margin-left:-8px; margin-top:-2px; min-height:36px;">
<i class="glyphicon glyphicon-search"></i>
</button>
</div>
<div class="row" align="center">
<div class="form-group" style="width: 50%" align="center">
<select id="dropdownForm" name="sortDropdown" class="form-control">
<option value="Name">Item Name</option>;
<option value="LocationID">Location</option>;
<option value="CategoryID">Category</option>;
<option value="Quantity">Quantity</option>;
<option value="LastQuantityUpdate">Most Recently Updated</option>;
</select>
</div>
</div>
</form>
And this is the PHP.
if (isset($_POST["submit"])) {
$searchResults = $_POST["searchResults"];
$dropdownSelection = $_GET["dropdownForm"];
if ($searchResults !== "") {
header("Location: test2.php?sortBy=".$dropdownSelection."&searchResults=".$searchResults);
}
else {
header("Location: test2.php?sortBy=".$dropdownSelection);
}
}
I have gotten this to work using the input from a search box, but cannot seem to get it to work with this dropdown menu and its selected option values.
Try adding a submit button...
Defines a submit button (for submitting the form).
<input type="submit">
defines a button for submitting the form data to a form-handler.
The form-handler is typically a server page with a script for processing input data.
The form-handler is specified in the form's action attribute.
Try using:
<form action="test1.php" method="POST" class="form-group">
<div class="row" align="center">
<div class="form-group" style="width: 50%" align="center">
<select id="dropdownForm" name="sortDropdown" class="form-control">
<option value="Name">Item Name</option>;
<option value="LocationID">Location</option>;
<option value="CategoryID">Category</option>;
<option value="Quantity">Quantity</option>;
<option value="LastQuantityUpdate">Most Recently Updated</option>;
</select>
</div>
</div>
<input type="submit" name="submit" value="Submit" />
</form>
Documentation: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/submit
If you're wanting to add parameters to the query string, you want to use a form with a GET method
instead. Then when you submit the form, its data will be added to the query string (ie for filtering/sorting search results):
<form action="test1.php" method="GET">
<select name="sort_by">
<option value="name">Name</option>
<!-- and so on -->
</select>
<button type="submit">Submit</button>
</form>
This will submit to test1.php?sort_by=name if the name option is selected.
You can add more fields as required. You'd also then access the values using $_GET
instead:
$sort_by = $_GET['sort_by'];
You are posting but are trying to access a get variable. Also, you are using the control id dropdownForm
and not the name sortDropdown
:
$dropdownSelection = $_GET["dropdownForm"];
Should be:
$dropdownSelection = $_POST["sortDropdown"];
Using error reporting:
error_reporting(E_ALL);
ini_set('display_errors', '1');
Would have given you this:
Notice: Undefined index: dropdownForm in /path/to/file.php on line X
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.