Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Noob warning.</p> <p>This is an interesting question for me, because I'll most-likely need to do it over the next few months, so I thought I take an hour and give it a shot.</p> <p>Here is the html (boring):</p> <pre><code>&lt;table class="centerme"&gt; &lt;tr&gt; &lt;td&gt;First Checkbox:&lt;input type="checkbox" id="checkboxtest"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>Here is the javascript:</p> <pre><code>&lt;script&gt; $(document).ready(function(e) { $('#checkboxtest').change(function(){ if( $('#checkboxtest').prop('checked') ) {checkboxstatus = "YES";} else {checkboxstatus = "NO";} $.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>Here is the backend php:</p> <pre><code>&lt;?php $checkboxstatus = $_POST['checkboxstatus']; $host = "xxxxxxxxxxxxxxxxx"; $user = "yyyyyyyyyyyyyyyyy"; $password = "zzzzzzzzzzzzzzzzzzzzzz"; $dbname = "qqqqqqqqqqqqqqqqqqqqqqqqqq"; $cxn = mysqli_connect($host,$user,$password,$dbname); if (mysqli_connect_errno()) {echo "No connection" . mysqli_connect_error();} $query = " UPDATE testtable SET lastname = '$checkboxstatus' WHERE localid = '158' "; $result = mysqli_query($cxn, $query) or die ("could not query database 1"); ?&gt; </code></pre> <p>When I uploaded it to my site, it seems to work (get an error message that I haven't figured out yet, but the db changes (checked it with MySQL Workbench).</p> <p>You could probably get away with just a $().POST, but I'm used to $ajax.</p> <p>It would be interesting to get some comments from the experts.</p> <p>PS - found the error - I should not have included the dataType in the ajax query.</p> <p>Works great now!</p> <p>Woohoo!</p>
 

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