Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging check box automatically changes the database record
    primarykey
    data
    text
    <p>in my database I have the filed status which is boolean type, </p> <p>thank I have a connection to the database </p> <pre><code> if (!$result = mysqli_query($con,"SELECT * FROM cursos")) { die("Error: " . mysqli_error($con)); } </code></pre> <p>and displaying the status of this field with checkboxes,</p> <pre><code> &lt;?php if($row['status'] == 1) { echo "&lt;td&gt;"."&lt;input type = 'checkbox' checked='checked' name ='complete' value= '1'/&gt;"."&lt;/td&gt;"; }else { echo "&lt;td&gt;"."&lt;input type = 'checkbox' name ='incomplete' value= '0'/&gt;"."&lt;/td&gt;"; } ?&gt; </code></pre> <p>So what I need to do is, once I check or uncheck any of thoose check boxes, the database field to be canged automatically withoud the need of submit button. </p> <p><strong>Okay I did edited my code to what you suggested and it s working now, here is the new code:</strong></p> <p>Ajax Code</p> <pre><code>&lt;script&gt; $(document).ready(function(e) { $('.checkboxtest').change(function(){ if( $('.checkboxtest').prop('checked') ) {checkboxstatus = '1';} else {checkboxstatus = '0';} $.ajax({ type: "POST", url: "checkboxtestbackend.php", data: {checkboxstatus: checkboxstatus}, }) .done(function(data, textStatus, jqXHR){alert(textStatus);}) .fail(function(jqXHR, textStatus, errorThrown){alert(jqXHR+"--"+textStatus+"--"+errorThrown);}); });//end change });//end ready &lt;/script&gt; </code></pre> <p>the html code is: </p> <pre><code>&lt;?php if($row['status'] == 1) { echo "&lt;td&gt;"."&lt;input type = 'checkbox' class='checkboxtest' checked='checked' name ='complete' value= '1'/&gt;"."&lt;/td&gt;"; }else { echo "&lt;td&gt;"."&lt;input type = 'checkbox' class='checkboxtest' name ='incomplete' value= '0'/&gt;"."&lt;/td&gt;"; } ?&gt; </code></pre> <p>and the php backend code is: </p> <pre><code>&lt;?php $checkboxstatus = $_POST['checkboxstatus']; $host = "localhost"; $user = "username"; $password = "password"; $dbname = "name"; $cxn = mysqli_connect($host,$user,$password,$dbname); if (mysqli_connect_errno()) {echo "No connection" . mysqli_connect_error();} $query = " UPDATE cursos SET status = '$checkboxstatus' WHERE id = '12'"; $result = mysqli_query($cxn, $query) or die ("could not query database 1"); ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload