如何在PHP中以检查顺序获取复选框值 [英] How to get the checkboxes value in checked order in php

查看:45
本文介绍了如何在PHP中以检查顺序获取复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在注册部分会有一些图像,并且每次刷新都会自动随机播放.用户应该选择一个或多个图像(勾选相应的复选框),它将存储在数据库中而不是密码.它将正常工作,问题出在登录页面中.我希望复选框的值处于选中状态.这意味着如果我先选中第二个复选框,然后再选中第一个复选框,那么值将获得相同的顺序.

In the registration part there will be some images and it will shuffle automatically in each refresh. user should select one or more images (tick the corresponding checkboxes) and it will stored in database instead of password.It will work correctly and problem is in login page.I want the values of checkboxes in checked order. that means if I checked second checkbox and then first box,then value will get the same order.

这是代码

<?php
include("config.php");
if(isset($_POST['s'])){
$username=$_POST['username'];
if(isset($_POST['chk1']))
{
 $t1=implode(',', $_POST['chk1']);
}
echo $t1;
$query=mysql_query("select *from register where username='$username' and password='$t1'");
$res=mysql_num_rows($query);
if($res>0)
{
session_start();
$_SESSION['usr']=$username;
header("location:userhome.php");
}
else
{
 echo mysql_error();
 }
 }
 ?>

<body>
<form action="" method="post">
<table width="200" border="0">
<th colspan="2" scope="row"><a href="register.php">
<font color="#FF0000">New Registration</font>      </a></th>
</tr>
<tr>
<th scope="row">Username</th>
<td><label for="username"></label>
<input type="text" name="username" id="username" /></td>
</tr>
<tr>
<th colspan="2" scope="row"><u>Choose Password</u></th>
</tr>
<tr>
<th scope="row">&nbsp;</th>
<td>&nbsp;</td>
</tr>
</table>
<div align="center">
<?php
$images = array(
'<input name="chk1[]" type="checkbox" value="1" />
<img src="images/images (4).jpg" alt="" width="234" height="212" />',
'<input name="chk1[]" type="checkbox" value="2"  />
<img src="images/images (6).jpg" alt="" width="234" height="212" />',
'<input name="chk1[]" type="checkbox" value="3" />
<img src="images/images (5).jpg" alt="" width="234" height="212" />', 
'<input name="chk1[]" type="checkbox" value="4" />
 <img src="images/drt.jpg" alt="" width="234" height="212" />',
'<input name="chk1[]"   type="checkbox" value="5" />
 <img src="images/rf.jpg" alt="" width="234" height="212" />',
'<input name="chk1[]" type="checkbox" value="6" />
<img src="images/yu.jpg" alt="" width="234" height="212" />', 
'<input name="chk1[]" type="checkbox" value="7" />
 <img src="images/ed.jpg" alt="" width="234" height="212" />'
  );
shuffle($images); // Randomize images array;
echo $images[0];
echo $images[1];
echo $images[2];
echo $images[3];
echo $images[4];
echo $images[5];
echo $images[6];
?>





推荐答案

PHP无法实现,您需要在JavaScript中实现.
使用JS创建内部Aray以存储复选框检查订单并发送它到PHP目标文件.

That's not possible with PHP, you need to implement it in JavaScript.
Create internal aray with JS to store checkbox checking order and send it to PHP target file.

在这里,我写了一个简单"但不太优雅的解决方案作为示例:

Here I written a "easy" but not elegant solution as example:

<form name="myform">

    <input id="chk1" name="chk1" type="checkbox" onclick="validate1()" /> test 1

    <input id="chk2" name="chk2" type="checkbox" onclick="validate2()" /> test 2

    <input id="chk3" name="chk3" type="checkbox" onclick="validate3()" /> test 3

    <input type="hidden" name="order"/>

    <input type="button" value="Click me" onclick="fin()">
</form>

    <script>
    var arrChecks = [];
    function validate1(){
        if (document.getElementById('chk1').checked) arrChecks.push( 1 );
    }

    function validate2(){
        if (document.getElementById('chk2').checked) arrChecks.push( 2 );
    }

    function validate3(){
        if (document.getElementById('chk3').checked) arrChecks.push( 3 );
    }

    function fin(){

        alert(arrChecks); // Show order, only for debug

       document.myform.order.value = arrChecks.toString(); // Array of checked options order
       document.myform.submit();                           // Send to php file
    }
    </script>

要使其更优雅,更优化,就需要动态迭代.但是这段代码有效.

To perform it more elegant and optimized, a dynamic iteration would be needed. But this code works.

致谢

这篇关于如何在PHP中以检查顺序获取复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆