【问题标题】:Storing checkbox values in order that they are clicked in PHP存储复选框值以便在 PHP 中单击它们
【发布时间】:2022-01-14 07:32:50
【问题描述】:

我有一个包含 4 个复选框的表单,我希望能够将单击的复选框保存在数据库中,但按顺序,它们会被单击。因此,例如,如果我按 1、3、4、2 的顺序单击复选框,它应该按该顺序保存在数据库中。目前,尽管我以不同的顺序进行了选择,但它只是以默认顺序保存了它。有没有办法做到这一点?

我的表格:

<?php
// Initialize the session
session_start();
// Store the submitted data sent
// via POST method, stored 
// Temporarily in $_POST structure.
$_SESSION['system_battery_runout']
    = $_POST['system_battery_runout'];
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Rank form</title>
</head>
<body>  
        <div class="div2">
        <h1>Which Appliances Do You Use The Most?</h1>
        <form action="insert.php" method="post">    
     <br />
<input  type="checkbox" style="height:20px; width:20px;" name="rank_appliances[]" value="Fridge" /><label style="font-size: 20px;"> Fridge</label><br />
<input  type="checkbox" style="height:20px; width:20px;" name="rank_appliances[]" value="Stove" /><label style="font-size: 20px;"> Stove</label><br />
<input  type="checkbox" style="height:20px; width:20px;" name="rank_appliances[]" value="TV" /><label style="font-size: 20px;"> TV</label><br />
<input  type="checkbox" style="height:20px; width:20px;" name="rank_appliances[]" value="Water Tank" /><label style="font-size: 20px;"> Water Tank</label><br />
<br/>
            <input type="submit" value="Submit" style="float: right;" >
        </form>
        </div>     
</body>
</html>

插入.php:

<?php
session_start();
?>
<!DOCTYPE html>
<html>

<head>
    <title>Insert Page</title>
</head>
</center>

<body>
    <div class="div2">
        <?php

        // serverfullname => localhost
        // userfullname => root
        // password => empty
        // database fullname => staff
        $conn = mysqli_connect("localhost", "root", "", "survey");
        // Check connection
        if ($conn === false) {
            die("ERROR: Could not connect. "
                . mysqli_connect_error());
        }
        $system_battery_runout = $_SESSION['system_battery_runout'];
        $rank_appliances = $_POST['rank_appliances'];
        $checkboxvalue = implode(",", $rank_appliances);


        $sql = $conn->prepare("INSERT INTO cus_survey( system_battery_runout,rank_appliances)  VALUES (?, ?)");
        $sql->bind_param('ss', $system_battery_runout, $checkboxvalue);

        if (mysqli_stmt_execute($sql)) {
            echo "<h3>Your survey was captured successfully. Thank You!";
        } else {
            echo "<h3>Sorry, something went wrong</h3> ";
        }

        // Close connection
        mysqli_close($conn);
        ?>
    </div>
</body>

</html>

【问题讨论】:

  • 表单元素system_battery_runout在哪里?数据库架构在哪里?如果您使用 ajax,您可以在单击每个请求时发送单独的请求,或者您希望以传统方式提交一次表单?无论哪种方式,您都需要 javascript
  • @ProfessorAbronsius 这只是一种形式,system_battery_runout 只是取自以前的形式。这个表单调用了一个insert.php,它只是将会话数据存储在db中
  • @ProfessorAbronsius 我希望先提交表格然后保存记录
  • 创建隐藏输入并在单击/选中复选框时使用 javascript 填充
  • 您需要 javascript 来处理操作。这是我找到的解决方案stackoverflow.com/a/60884815/14032355

标签: javascript php html mysql


【解决方案1】:

一个新的隐藏字段,可以使用 Javascript 轻松填充,并在提交表单时产生点击顺序。

let tmp=[];
let hidden=document.querySelector('input[name="rank_sequence"]');

document.querySelectorAll('input[name="rank_appliances[]"]').forEach( input=>input.addEventListener('click',function(e){
  /* Add the clicked value if checked or remove if unchecked */
  if( this.checked )tmp.push( this.value );
  else{
    if( tmp.includes( this.value ) )tmp.splice( tmp.indexOf( this.value ), 1 );
  }
  //add to the hidden input value
  hidden.value=tmp.join(',');
  console.info(hidden.value);
}));
<div class="div2">
  <h1>Which Appliances Do You Use The Most?</h1>
  <form action="insert.php" method="post">
    <br />
    <input type="checkbox" style="height:20px; width:20px;" name="rank_appliances[]" value="Fridge" /><label style="font-size: 20px;"> Fridge</label><br />
    <input type="checkbox" style="height:20px; width:20px;" name="rank_appliances[]" value="Stove" /><label style="font-size: 20px;"> Stove</label><br />
    <input type="checkbox" style="height:20px; width:20px;" name="rank_appliances[]" value="TV" /><label style="font-size: 20px;"> TV</label><br />
    <input type="checkbox" style="height:20px; width:20px;" name="rank_appliances[]" value="Water Tank" /><label style="font-size: 20px;"> Water Tank</label><br />
    <br/>
    <input type="submit" value="Submit" style="float: right;">
    <input type='hidden' name='rank_sequence' />
  </form>
</div>

也许要处理新的输入元素,您可以执行以下操作(虽然未经测试)

if( $_SERVER['REQUEST_METHOD']=='POST' && isset(
    $_SESSION['system_battery_runout'],
    $_POST['rank_sequence']
)){

    $conn = new mysqli("localhost", "root", "", "survey");
    $stmt=$conn->prepare('insert into `cus_survey` ( `system_battery_runout`, `rank_appliances` ) values (?, ?)');
    $stmt->bind_param('ss', $_SESSION['system_battery_runout'], $_POST['rank_sequence'] );
    $stmt->execute();
    
    $msg=( $stmt->affected_rows==1 ) ? 'Your survey was captured successfully. Thank You!' : 'Sorry, something went wrong';
    $stmt->close();
    $conn->close();
    
    printf('<h3>%s</h3>',$msg);
    
}

【讨论】:

  • 它仍然按默认顺序存储,知道为什么吗?
  • 这个新字段叫做rank_sequence——你需要在PHP中处理才能添加到数据库中。但是您还没有展示 PHP 处理方面,所以除了您之外,这对所有人来说都是一个谜
  • 我已更新问题以包含插入 PHP 脚本
  • 你没有处理隐藏的输入值
  • 我该怎么做呢?我是 PHP 新手,我很抱歉
猜你喜欢
  • 2013-12-27
  • 2012-01-25
  • 1970-01-01
  • 1970-01-01
  • 2015-02-18
  • 2019-06-29
  • 1970-01-01
  • 2021-05-17
  • 2018-09-16
相关资源
最近更新 更多