dongliyi967823 2018-05-08 19:27
浏览 132
已采纳

PHP复选框返回值(选中/未选中)并更新

I have a form that submits a load of checkboxes - This works fine.

I then have a form i can open which will display said checkboxes and will be checked or not checked based on the SQL query ran.

SQL query runs ok with echo but I cannot get the box to show as checked. After this I want to be able to check uncheck the boxes and update via an SQL.

<?php
    session_start();
    ini_set('display_errors', 1); error_reporting(-1);
    $data = $_GET['id'];
    require 'connect.php';
    $sql1 = "SELECT id, phone, email, pager
    FROM [dbo].[preference] WHERE id = '$data'";
    $stmt1 = sqlsrv_query($con,$sql1);
    if( $stmt === false) {
    die( print_r( sqlsrv_errors(),true));
}   
        while ($row1 = sqlsrv_fetch_array($stmt1)){
        $id = $row1['id'];
        $phone = $row1['phone'];
        $email = $row1['email'];
        $pager = $row1['pager'];
}
?>


<td><input type="checkbox" name="phone" class="check" value="1">phone</td>
<td><input type="checkbox" name="email" class="check" value="1">email</td>
<td><input type="checkbox" name="pager" class="check" value="1">pager</td>

How can I resolve this?

  • 写回答

2条回答 默认 最新

  • dplht39359 2018-05-08 19:39
    关注

    I'm still not really sure I understand the question...

    You load the script after an update. The rendering reflects the current state at that time. You submit to the script, perform the update, then reload... which then renders that updated state...

    <?php
            ...
            $id = $row1['id'];
            $phone = $row1['phone'];
            $email = $row1['email'];
            $pager = $row1['pager'];
    }
    ?>
    
    
    <td><input type="checkbox" name="phone" class="check" value="1" <?= (!empty($phone)) ? 'CHECKED' : '' ?>>phone</td>
    ...
    

    High level...

    //pseudo code for a CRUD script...
    
    //is this a POST?
    if (isset($_POST['submit')) {
      //validate user input, never trust the user, beware SQL injection...
    
      //if passes validation update the values in the database
    
      //if not, raise error flags, re-display the form (you'll see you need to use the supplied POST data, that failed to validate, as the inputs values or you'll lose the provided input, think it will make sense when you het here...)
    }
    
    //here could be a POST or initial load... could even flow here to provide confirmation of update and show new values
    
    //load current data from database
    
    //render form based off vurrent values
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊