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条)

报告相同问题?