douzao2590 2017-02-22 01:08
浏览 49
已采纳

在php中从动态表单中检索数据

How I can process a dynamic form. Number of options will vary by the number of options from the database. Based on yes/no value will be updated in the database is user want to test that parameter. Here is my code

<form action="?userid=<?php echo $userid;?>&tankid=<?php echo $tankid;?>&action=update" method="POST">
    <table width="100%">
        <tr>
            <th >Parameter name:</th>
            <th >Do you want to test it?</th>
        </tr>
        <?php

        while ($row = $records_param_info->fetch(PDO::FETCH_ASSOC)){
            $checked_yes = "";
            $checked_no = "";
            echo "<tr>";
            echo "<td>";
            echo $row['paramname'];
            echo "</td><td>";
            if ($row['active'] == 1){
                $checked_yes = "checked";
                $checked_no = "";
            } else {
                $checked_yes = "";
                $checked_no = "checked";
            }
            echo '<label>Yes</label><input type="radio" name='.$row['parameterid'].' '.$checked_yes.' value="1">';
            echo '<label>No</label><input type="radio" name='.$row['parameterid'].' '.$checked_no.' value="0">';
            echo "<td></tr>";
        }

        ?>


    </table>
    <br>
    <center><input type="submit" name="Update" value ="Update"></center>
    </form>

My question will be how to retrieve the data and processes it to the databse since the "name" will be always different and the number will be different.

  • 写回答

1条回答 默认 最新

  • douniuta4783 2017-02-22 03:29
    关注

    If you are having problems distinguishing between your radio buttons and other form fields, you could modify the names of your radio buttons slightly to get them all into a single array:

    '<label>Yes</label><input type="radio" name=params[' . $row['parameterid'] . '] ' . $checked_yes . ' value="1">';
    

    using the following dummy data:

    $paramsFromDB = Array(
        Array('parameterid' => 101, 'paramname' => 'param one', 'active' => 0),
        Array('parameterid' => 202, 'paramname' => 'param two', 'active' => 1),
        Array('parameterid' => 303, 'paramname' => 'param three', 'active' => 0),
        Array('parameterid' => 404, 'paramname' => 'param four', 'active' => 1)
    );
    

    Your resulting html would look like this:

    <table width="100%">
            <tr>
                <th>Parameter name:</th>
                <th>Do you want to test it?</th>
            </tr>
            <tr>
                <td>param one</td>
                <td>
                    <label>Yes</label><input type="radio" name=params[101] value="1">
                    <label>No</label><input type="radio" name=params[101] checked value="0">
                <td>
            </tr>
            <tr>
                <td>param two</td>
                <td>
                    <label>Yes</label><input type="radio" name=params[202] checked value="1">
                    <label>No</label><input type="radio" name=params[202] value="0">
                <td>
            </tr>
            <tr>
                <td>param three</td>
                <td>
                    <label>Yes</label><input type="radio" name=params[303] value="1">
                    <label>No</label><input type="radio" name=params[303] checked value="0">
                <td>
            </tr>
            <tr>
                <td>param four</td>
                <td>
                    <label>Yes</label><input type="radio" name=params[404] checked value="1">
                    <label>No</label><input type="radio" name=params[404] value="0">
                <td>
            </tr>
        </table>
    

    your $_POST data would look like this:

    array(2) {
      ["params"]=>
      array(4) {
        [101]=>
        string(1) "0"
        [202]=>
        string(1) "1"
        [303]=>
        string(1) "0"
        [404]=>
        string(1) "1"
      }
      ["Update"]=>
      string(6) "Update"
    }
    

    So you have the data in the form param[parameterid] = active. So it's a simple matter to update your database accordingly:

    if (count($_POST['params']) > 0) {
    
        $sql = $db->prepare('UPDATE `table_name` SET `active` = ? WHERE `parameterid` = ?');
    
        foreach ($_POST['params'] as $parameterid => $paramvalue) {
            $sql->execute(Array($paramvalue, $parameterid));
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在hololens1上运行unity项目只有空窗口
  • ¥25 TABLEAU PREP无法打开
  • ¥15 关于#c语言#的问题:求完整代码条件好说
  • ¥15 (需要远程,AI不回)VB6二进制文件转换成功,但是C#转换总是失败
  • ¥15 关于#matlab#的问题:有没有什么其他办法能够保证不退出进程(相关搜索:matlab调用)
  • ¥15 依据报错在原代吗格式的基础上解决问题
  • ¥15 在虚拟机中安装flash code
  • ¥15 单片机stm32f10x编写光敏电阻调节3.3伏大功率灯亮度(光强越大灯越暗,白天正常光强灯不亮,使用ADC,PWM等模块)望各位找一下错误或者提供一个可实现功能的代码
  • ¥20 verilog状态机方法流水灯
  • ¥15 pandas代码实现不了意图