如何在 php 中按选中顺序获取复选框值


How to get the checkboxes value in checked order in php

在注册部分会有一些图像,每次刷新都会自动洗牌。 用户应选择一个或多个图像(勾选相应的复选框),它将存储在数据库中而不是密码中。它将正常工作,问题出在登录页面中。我希望复选框的值按选中顺序排列。这意味着如果我选中第二个复选框,然后选中第一个框,那么值将获得相同的顺序。

这是代码

<?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 目标文件。

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

<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>

为了更优雅和优化地执行它,需要进行动态迭代。 但是这段代码有效。

问候