我在前台页面上做了个表单循环输出了复选框,并用js对其进行控制。代码如下:<form action="voit.php" method="post" name="voit" id="form2">
<table width="626" boder="1" id="xu">
<?php
require("config/conn.php");
$mysql = @mysql_connect(HOST,USER,PASS) or die ("数据库连接失败");
mysql_select_db(DBNAME,$mysql);
$sql = "select * from baby_bb ";
$result = mysql_query($sql, $mysql);
$i=1;
//for($i=1;$i<=8;$i++){
//echo"<tr>";
//echo"<td><p><img src='./photos/1.jpg' width='140' height='110' /></p><p>编号:001<input type='checkbox' name='check' value='1'/></p></td>";
//echo"<td><p><img src='./photos/2.jpg' width='140' height='110' /></p><p>编号:002<input type='checkbox' name='check' value='2'/></p></td>";
//echo"<td><p><img src='./photos/3.jpg' width='140' height='110' /></p><p>编号:003<input type='checkbox' name='check' value='3'/></p></td>";
//echo"<td><p><img src='./photos/4.jpg' width='140' height='110' /></p><p>编号:004<input type='checkbox' name='check' value='4'/></p></td>";
//echo"</tr>";
//}
while($row=mysql_fetch_assoc($result)){
$id = $row["id"];
$img = $row["img_path"];
$img_s = $row["img_s_path"];
$b_id = $row["bb_id"];
if($i%4==1){
echo"<tr>";
}
echo"<td>";
echo"<p><a href=".$img."><img src='".$img_s."' width='140' height='140' /></a></p><p>编号:".$b_id."<input type='checkbox' name='check[]' value='".$id."'></p>";
echo"</td>";
if($i%4==0){
echo"</tr>";
}
$i++;
}
?>
</table>
<h3><input type="submit" name="submit2" id="submit2" value="提交" onclick="check()"/><input type="hidden" name="action1" value="voit"/></h3>
</form>
js代码:
function check(){
var num=0;
var box=document.getElementsByName("check[]");
for(var i=0;i<box.length;i++){
if(box[i].checked){
num++;
}
}
if(num!=12){alert("您所选的宝宝不满12位或超过12位,请选择12位可爱的宝宝!");
window.location.href='index.php';}
return true;
}
php处理代码:<?php
session_start();
require("config/conn.php");
$username = $_SESSION['username'];
$action = $_REQUEST['action1'];
$mysql=@mysql_connect(HOST,USER,PASS) or die ("数据库连接失败");
mysql_select_db(DBNAME,$mysql);
mysql_query("set names utf8");
if($action=='voit'){
if(isset($username)){
//echo "你已登录可以投票";
$i=1;
if($sumbit="提交"){
$sql = "select * from baby_user where num = '$username' and counst = $i";
$result = mysql_query($sql, $mysql);
//echo $result;
$num = mysql_num_rows($result);
if($num>0){
echo"<script>alert('你已经投过票,请不要重复投票');history.go(-1);</script>";
}else{
if(!empty($_POST['check'])){
foreach($_POST['check'] as $id){
$res = mysql_query("update baby_bb set sum=sum+1 where id=$id");
$us = mysql_query ("update baby_user set counst=$i where num = $username");
echo"<script>alert('投票成功!');location.href='index.php';</script>";
}
}
}
}
}else{
echo "<script>alert('您还没有登录,请先登录');history.go(-1);</script>";
}
}else{
echo"<script>alert('非法登录'); history.go(-1);</script>";
}我想问的是为甚么我登陆后选了少于12个框或者多于12个框。都会弹出js的提示框。但确定后都会弹出提示投票成功,并也写入了数据库?怎样改才能实现大于或小于12个框都弹出提示框,然后不会进行php判断处理直接重新进行选择?
<table width="626" boder="1" id="xu">
<?php
require("config/conn.php");
$mysql = @mysql_connect(HOST,USER,PASS) or die ("数据库连接失败");
mysql_select_db(DBNAME,$mysql);
$sql = "select * from baby_bb ";
$result = mysql_query($sql, $mysql);
$i=1;
//for($i=1;$i<=8;$i++){
//echo"<tr>";
//echo"<td><p><img src='./photos/1.jpg' width='140' height='110' /></p><p>编号:001<input type='checkbox' name='check' value='1'/></p></td>";
//echo"<td><p><img src='./photos/2.jpg' width='140' height='110' /></p><p>编号:002<input type='checkbox' name='check' value='2'/></p></td>";
//echo"<td><p><img src='./photos/3.jpg' width='140' height='110' /></p><p>编号:003<input type='checkbox' name='check' value='3'/></p></td>";
//echo"<td><p><img src='./photos/4.jpg' width='140' height='110' /></p><p>编号:004<input type='checkbox' name='check' value='4'/></p></td>";
//echo"</tr>";
//}
while($row=mysql_fetch_assoc($result)){
$id = $row["id"];
$img = $row["img_path"];
$img_s = $row["img_s_path"];
$b_id = $row["bb_id"];
if($i%4==1){
echo"<tr>";
}
echo"<td>";
echo"<p><a href=".$img."><img src='".$img_s."' width='140' height='140' /></a></p><p>编号:".$b_id."<input type='checkbox' name='check[]' value='".$id."'></p>";
echo"</td>";
if($i%4==0){
echo"</tr>";
}
$i++;
}
?>
</table>
<h3><input type="submit" name="submit2" id="submit2" value="提交" onclick="check()"/><input type="hidden" name="action1" value="voit"/></h3>
</form>
js代码:
function check(){
var num=0;
var box=document.getElementsByName("check[]");
for(var i=0;i<box.length;i++){
if(box[i].checked){
num++;
}
}
if(num!=12){alert("您所选的宝宝不满12位或超过12位,请选择12位可爱的宝宝!");
window.location.href='index.php';}
return true;
}
php处理代码:<?php
session_start();
require("config/conn.php");
$username = $_SESSION['username'];
$action = $_REQUEST['action1'];
$mysql=@mysql_connect(HOST,USER,PASS) or die ("数据库连接失败");
mysql_select_db(DBNAME,$mysql);
mysql_query("set names utf8");
if($action=='voit'){
if(isset($username)){
//echo "你已登录可以投票";
$i=1;
if($sumbit="提交"){
$sql = "select * from baby_user where num = '$username' and counst = $i";
$result = mysql_query($sql, $mysql);
//echo $result;
$num = mysql_num_rows($result);
if($num>0){
echo"<script>alert('你已经投过票,请不要重复投票');history.go(-1);</script>";
}else{
if(!empty($_POST['check'])){
foreach($_POST['check'] as $id){
$res = mysql_query("update baby_bb set sum=sum+1 where id=$id");
$us = mysql_query ("update baby_user set counst=$i where num = $username");
echo"<script>alert('投票成功!');location.href='index.php';</script>";
}
}
}
}
}else{
echo "<script>alert('您还没有登录,请先登录');history.go(-1);</script>";
}
}else{
echo"<script>alert('非法登录'); history.go(-1);</script>";
}我想问的是为甚么我登陆后选了少于12个框或者多于12个框。都会弹出js的提示框。但确定后都会弹出提示投票成功,并也写入了数据库?怎样改才能实现大于或小于12个框都弹出提示框,然后不会进行php判断处理直接重新进行选择?
var num=0;
var box=document.getElementsByName("check[]");
for(var i=0;i<box.length;i++){
if(box[i].checked){
num++;
}
}
if(num!=12){alert("您所选的宝宝不满12位或超过12位,请选择12位可爱的宝宝!");window.location.href='index.php';
return false;}
return true;
}
var num=0;
var box=document.getElementsByName("check[]");
for(var i=0;i<box.length;i++){
if(box[i].checked){
num++;
}
}
if(num!=12){alert("您所选的宝宝不满12位或超过12位,请选择12位可爱的宝宝!");
window.location.href='index.php';//去掉并加return false;}
return true;
}
请问jquery要怎么写啊?我不会。
<input type="submit" name="submit2" id="submit2" value="提交" onclick="return check()"/>
function check(){
...
if(num!=12){
alert(...);
return false;
}
return true;
}