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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀