dsfbnhc4373 2017-05-20 01:12
浏览 77
已采纳

循环Mysql更新复选框从哪里开始

I have a question how can I update the database if person unchecks or check a checkbox and update the boolean field in mysql? For days stuck with this problem, because problem I don't know how to make a form or check if it is validate inside of a while loop here is my code:

<?
    $result= mysql_query("SELECT * FROM cars");
    $counter = 1;

    while($row = mysql_fetch_array($result)) { 
        echo '<tr>
            <td>' . $counter++ . '</td>
            <td><input type="checkbox"';  
            if ($row['show'] == true) {  
                echo 'checked></td>'; 
            } else { 
                echo 'unchecked></td>'; 
            }
            echo '<td><img src="../xml/'.$row['cars_id'].'-1.JPG"  width="120px"></td>';        
            echo "<td><h3> ", $row['brand']," " . $row['model'], " " . $row['type'], 
             " &euro; " . $row['price'], " </h3></td>";
        echo '</tr>';
    }
?>

p.s. I am aware of the mysql to mysqli or pdo but it is a HUGE script...

  • 写回答

3条回答 默认 最新

  • drdr123456 2017-05-20 01:27
    关注

    Oh! No issue here is the solution: Try using jquery and ajax. For example.

    if (  $("#chkbx").prop('checked') == true) {
        // do ajax call to update the database
    } else {
        // do anything if check box is unchecked
    }
    

    I am not writing the ajax call just see the jquery ajax manual. If you face any problem come back.

    See the above code will create a event listener in the DOM so that when the check box is checked a event should fire. Now I am extending my code to show you a ajax call.

    if (  $("#chkbx").prop('checked') == true) {
        $.ajax ({
            method: "POST", // type:post or get
            url: "test.php", // your php file
            data: { name: "test"} // data that I want to send
        }).done(function( msg ) {
            alert( "Data Saved: " + msg ); // after success show msg
        })
    }
    

    Now in the php file do the updating part of database or anything you want. The done function will show the msg you returned if the php file execute properly. There are numerous functions in ajax that you can use.Also try to use jquery it's handy and easy to use.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)