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 问题重发,R语言:代码运行过程中出现如下警告,请求如何解决!
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 苍穹外卖拦截器token为null
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关