我正在处理这个脚本,它将根据选中的复选框过滤sql结果。我的桌子就是这样设置的。
id venue imageurl showingads 2kandunder 2kto4k 4kandup
1 venue1 myurl.com yes yes
2 venue2 myurl.com yes yes
3 venue3 myurl.com no yes
4 venue4 myurl.com yes yes
所有代码都在同一页面上。这是html ..
<form id="form" method="post" action="">
<input type="checkbox" name="2kandunder" class="checkbox" <?=(isset($_POST['2kandunder'])?' checked':'')?>/> 2kandunder<br>
<input type="checkbox" name="2kto4k" class="checkbox" <?=(isset($_POST['2kto4k'])?' checked':'')?>/> 2k to 4k<br>
<input type="checkbox" name="4kandup" class="checkbox" <?=(isset($_POST['4kandup'])?' checked':'')?>/> 4k and up<br>
</form>
Javascript代码......
<script type="text/javascript">
$(function(){
$('.checkbox').on('change',function(){
$('#form').submit();
});
});
</script>
和php ...
<?php
$servername = "localhost";
$username = "myusername";
$password = "mypassword";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST["2kandunder"])) {
$arguments[] = "`2kandunder` = 'yes'";
}
if (isset($_POST["2kto4k"])) {
$arguments[] = "`2kto4k` LIKE 'yes'";
}
if (isset($_POST["4kandup"])) {
$arguments[] = "4kandup LIKE '%yes%'";
}
if(!empty($arguments)) {
$str = implode(' or ',$arguments);
$qry = "SELECT id, venue, imageurl FROM ads where " . $str . "";
$paginate = new pagination($page, $qry, $options);
} else {
//Whatever happens when there's none checked.
$sql = "SELECT id, venue, imageurl FROM ads WHERE `showingads` = 'yes'";
}
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<table class='tablebox' width='940' border='1' cellspacing='5'>
<tr><td width='75'>".$row["venue"]."</td></tr>
<tr>
<td width='10'><a href='post_click.php?id=".$row["id"]."'> <img src='".$row["imageurl"]."'></a></td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
当我运行此页面时,查询显示我希望在页面首次加载时发生的情况。但是,当我检查任何复选框时,没有任何反应。这是整个代码,因为它在页面上。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="JavaScript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
</head>
<html>
<body>
<form id="form" method="post" action="">
<input type="checkbox" name="2kandunder" class="checkbox" <?=(isset($_POST['2kandunder'])?' checked':'')?>/> 2k and under<br>
<input type="checkbox" name="2kto4k" class="checkbox" <?=(isset($_POST['2kto4k'])?' checked':'')?>/> 2k to 4k<br>
<input type="checkbox" name="4kandup" class="checkbox" <?=(isset($_POST['4kandup'])?' checked':'')?>/> 4k and up<br>
</form>
<script type="text/javascript">
$(function(){
$('.checkbox').on('change',function(){
$('#form').submit();
});
});
</script>
<?php
$servername = "localhost";
$username = "myusername";
$password = "mypassword";
$dbname = "mydb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_POST["2kandunder"])) {
$arguments[] = "`2kandunder` = 'yes'";
}
if (isset($_POST["2kto4k"])) {
$arguments[] = "`2kto4k` LIKE 'yes'";
}
if (isset($_POST["4kandup"])) {
$arguments[] = "4kandup LIKE '%yes%'";
}
if(!empty($arguments)) {
$str = implode(' or ',$arguments);
$qry = "SELECT id, venue, imageurl FROM ads where " . $str . "";
$paginate = new pagination($page, $qry, $options);
} else {
//Whatever happens when there's none checked.
$sql = "SELECT id, venue, imageurl FROM ads WHERE `showingads` = 'yes'";
}
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<table class='tablebox' width='940' border='1' cellspacing='5'>
<tr><td width='75'>".$row["venue"]."</td></tr>
<tr>
<td width='10'><a href='post_click.php?id=".$row["id"]."'> <img src='".$row["imageurl"]."'></a></td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
我的第一个想法是1. javascript根本没有执行? 2.我的$参数格式错误,我的数据库设置如何? (我有3种不同的参数来尝试测试不同的格式以确定我是否可以让它工作)
注意:我从中学到的教程的php部分有if(isset($ _ POST [&#34; 2kandunder&#34;])这样的参数。我有这样的:if(isset($ _ POST [&# 34; 2kandunder&#34;]))最后加上括号,因为我收到了每个if语句的语法错误。如果这可能是问题,为什么我为每个if语句行得到语法错误?
所有这些代码和喋喋不休,但我觉得我很亲密。有人可以帮我弄清问题是什么?
修改 列设置如下..
Field Type Null Key Default Extra
id int(5) NO PRI NULL auto_increment
venue varchar(100) NO NULL
imageurl varchar(150) NO NULL
showingads varchar(5) NO NULL
2kandunder varchar(5) NO NULL
2kto4k varchar(5) NO NULL
4kandup varchar(5) NO NULL
更新 所以我现在有复选框工作,但现在当我加载页面并单击顶部复选框(2kandunder)时,它返回0结果。当没有选中复选框时,else sql仍可用。我将第一个if语句更改为
if (isset($_POST["2kandunder"])) {
$arguments[] = "`showingads` = 'yes'";
当选中第一个(2kandunder)框时,应使其与else sql查询的查询相同。仍有0结果显示。所以我回复了$ qry来显示当选中第一个复选框时正在运行的查询,它与else $ sql查询完全相同但仍然返回0结果?如果完全相同的查询适用于else查询,这怎么可能?
上次更新 问题是if $ results它只显示sql查询的结果,感谢任何帮助过的人!
答案 0 :(得分:0)
你是对的。 javascript根本没有执行。您正在使用jquery 1.4.1。但是&#34; on&#34;自1.7以来可用 我希望这会对你有所帮助。