使用Javascript过滤数据库中的结果,这将使DIV根据新参数重新加载其内容(Filtering results from a DB with a Javascript that will make a DIV reload it's contents based on new arguments)
我需要创建一个带有Checkboxes的表单来过滤从MySQL表中提取的结果。 理想情况下,在单击特定复选框后,将重新加载DIV,运行相应的PHP和PDO查询以筛选所选条件。
到目前为止这是我的代码,但它似乎不起作用......
有什么建议么?
谢谢!
<script> $(document).ready(function() { $("#colors").change(function() { console.log("changed..."); var data = $(this).serialize(); console.log(data); $("#indexMain").load("index.php?data="+data) }); }) </script> <div class="colors"> <form method="post" action="index.php"> <?php $colors = mysql_query("SELECT DISTINCT color_base1 FROM item_descr ORDER BY color_base1"); while ($colorBoxes = mysql_fetch_array($colors)) { echo "<input type='checkbox' name='color' value='".$colorBoxes[color_base1]."' /> ".$colorBoxes[color_base1]."<br />"; } ?> <input type="submit" onclick="document.formName.submit();" /> </form> </div>我得到我的div中的值如下:
$color = $_POST['color']; if ($selectedColor == '') { $items = mysql_query("SELECT * FROM item_descr"); } else { $items = mysql_query("SELECT * FROM item_descr ORDER BY $color"); }I need to create a form with Checkboxes to filter results extracted from a MySQL table. Ideally, after clicking on specific checkboxes, a DIV will be reloaded running the appropriate PHP and PDO query to filter on the selected criteria.
This is my code so far, but it doesn't seem to work...
Any suggestions?
Thanks!
<script> $(document).ready(function() { $("#colors").change(function() { console.log("changed..."); var data = $(this).serialize(); console.log(data); $("#indexMain").load("index.php?data="+data) }); }) </script> <div class="colors"> <form method="post" action="index.php"> <?php $colors = mysql_query("SELECT DISTINCT color_base1 FROM item_descr ORDER BY color_base1"); while ($colorBoxes = mysql_fetch_array($colors)) { echo "<input type='checkbox' name='color' value='".$colorBoxes[color_base1]."' /> ".$colorBoxes[color_base1]."<br />"; } ?> <input type="submit" onclick="document.formName.submit();" /> </form> </div>and I get the value in my div as follows:
$color = $_POST['color']; if ($selectedColor == '') { $items = mysql_query("SELECT * FROM item_descr"); } else { $items = mysql_query("SELECT * FROM item_descr ORDER BY $color"); }最满意答案
Colors是您的div的名称,但我认为您希望在您的名称为color复选框上触发更改,而不是s 。
$(document).ready(function() { $("input[type=checkbox][name=color]").change(function() { console.log("changed..."); var data = $(this).serialize(); console.log(data); $("#indexMain").load("index.php?data="+data) }); })Colors is the name of your div, but I presume you want the on-change to be fired on your checkboxes, whose names are color, without an s.
$(document).ready(function() { $("input[type=checkbox][name=color]").change(function() { console.log("changed..."); var data = $(this).serialize(); console.log(data); $("#indexMain").load("index.php?data="+data) }); })更多推荐
发布评论