How can I make the variable $Category in the mysql query below equal the value chosen in the dropdown list below that?
如何使下面的mysql查询中的变量$ Category等于下面下拉列表中选择的值?
It seems like a simple concept that can't seem to figure out. It would also be great if the page automatically runs the new query when the dropdown list option changes. Furthermore, it should default to showing all records when the page first loads.
这似乎是一个似乎无法弄清楚的简单概念。如果页面在下拉列表选项更改时自动运行新查询,那也会很棒。此外,它应默认显示页面首次加载时的所有记录。
MYSQL PHP QUERY
MYSQL PHP QUERY
$query = mysql_query("SELECT * FROM tblClients WHERE tblclients.package = 'standard' and tblclients.category = '$Category' LIMIT 0, 9", $connection);
DROPDOWN LIST
下拉列表
<section class="main">
<div class="wrapper">
<div id="dd" class="wrapper-dropdown-3" tabindex="1">
<span>View By Category</span>
<ul class="dropdown">
<?php while ($rows = mysql_fetch_array($query_category)) { ?>
<li><a class="<?php echo $rows['category']; ?>"><?php echo $rows['category']; ?></a></li>
<?php } ?>
</ul>
</div>
</div>
</section>
1 个解决方案
#1
4
If you do not want to use a form and pass the value of an option from a select, and instead want the user to click a link in a list, you can append the value to the URL and pass it as a get parameter.
如果您不想使用表单并从选择中传递选项的值,而是希望用户单击列表中的链接,则可以将该值附加到URL并将其作为get参数传递。
Change:
更改:
<li><a class="<?php echo $rows['category']; ?>"><?php echo $rows['category']; ?></a></li>
To:
至:
<li><a href="?category=<?php echo $rows['category']; ?>" class="<?php echo $rows['category']; ?>"><?php echo $rows['category']; ?></a></li>
You can then access that variable as a $_GET variable. Like:
然后,您可以将该变量作为$ _GET变量进行访问。喜欢:
$selectedCategory = $_GET['category'];
$query = mysql_query("SELECT * FROM tblClients WHERE tblclients.package = 'standard' and tblclients.category = " . $selectedCategory . " LIMIT 0, 9", $connection);
Be sure to sanitize your user input data, as well.
务必确保清理用户输入数据。
Response to your comment:
回复你的评论:
If you want to use a select and options, you can use a form. The form's action will be the URL of the PHP script that will process the selected values of the form elements. The form's method can be either GET (and you can process the variable similarly to how I outlined previously), or it can be POST, and you can access the variable with the $_POST array rather than the $_GET array.
如果要使用选择和选项,可以使用表单。表单的操作将是PHP脚本的URL,它将处理表单元素的选定值。表单的方法可以是GET(你可以像我之前概述的那样处理变量),也可以是POST,你可以使用$ _POST数组而不是$ _GET数组来访问变量。
<form name="form1" action="" method="GET">
<select name="category">
<option value="home">Home</option>
<option value="work">Work</option>
<option value="school">School</option>
</select>
<input type="submit" value="Submit"/>
</form>
Or, you can use javascript to redirect to your processing code onchange of the select:
或者,您可以使用javascript重定向到更改选择的处理代码:
<select name="category" onchange="location = window.location.href + "?category="+this.selectedIndex.value;">
<option value="home">Home</option>
<option value="work">Work</option>
<option value="school">School</option>
</select>
And process the $_GET variable in the same way that I outlined previously.
并按照我之前概述的方式处理$ _GET变量。
#1
4
If you do not want to use a form and pass the value of an option from a select, and instead want the user to click a link in a list, you can append the value to the URL and pass it as a get parameter.
如果您不想使用表单并从选择中传递选项的值,而是希望用户单击列表中的链接,则可以将该值附加到URL并将其作为get参数传递。
Change:
更改:
<li><a class="<?php echo $rows['category']; ?>"><?php echo $rows['category']; ?></a></li>
To:
至:
<li><a href="?category=<?php echo $rows['category']; ?>" class="<?php echo $rows['category']; ?>"><?php echo $rows['category']; ?></a></li>
You can then access that variable as a $_GET variable. Like:
然后,您可以将该变量作为$ _GET变量进行访问。喜欢:
$selectedCategory = $_GET['category'];
$query = mysql_query("SELECT * FROM tblClients WHERE tblclients.package = 'standard' and tblclients.category = " . $selectedCategory . " LIMIT 0, 9", $connection);
Be sure to sanitize your user input data, as well.
务必确保清理用户输入数据。
Response to your comment:
回复你的评论:
If you want to use a select and options, you can use a form. The form's action will be the URL of the PHP script that will process the selected values of the form elements. The form's method can be either GET (and you can process the variable similarly to how I outlined previously), or it can be POST, and you can access the variable with the $_POST array rather than the $_GET array.
如果要使用选择和选项,可以使用表单。表单的操作将是PHP脚本的URL,它将处理表单元素的选定值。表单的方法可以是GET(你可以像我之前概述的那样处理变量),也可以是POST,你可以使用$ _POST数组而不是$ _GET数组来访问变量。
<form name="form1" action="" method="GET">
<select name="category">
<option value="home">Home</option>
<option value="work">Work</option>
<option value="school">School</option>
</select>
<input type="submit" value="Submit"/>
</form>
Or, you can use javascript to redirect to your processing code onchange of the select:
或者,您可以使用javascript重定向到更改选择的处理代码:
<select name="category" onchange="location = window.location.href + "?category="+this.selectedIndex.value;">
<option value="home">Home</option>
<option value="work">Work</option>
<option value="school">School</option>
</select>
And process the $_GET variable in the same way that I outlined previously.
并按照我之前概述的方式处理$ _GET变量。