Note that there are some explanatory texts on larger screens.

plurals
  1. POjavascript: check box value stays as 1 instead of 0 when unchecked
    text
    copied!<p>Hi am learning phonegap recently. I have this code below. My intention is to get user record from the mysql and then come out with some tick box where the user can update and then write directly in to the mysql.</p> <p>The process is working fine, just the funny thing is that the value of the checkbox, even if I un-check it, it still stays as 1 instead of 0</p> <p>Can please advice where I went wrong? FYI, I am new to javascript too, so likely it's just js error that is causing this.</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;PhoneGap Ajax Sample&lt;/title&gt; &lt;script type="text/javascript" src="phonegap.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function appReady(){ var myId = sessionStorage.getItem('myId'); var ajax = new XMLHttpRequest(); ajax.open("GET","domain/system_setup.php?user_id="+myId+"",true); ajax.send(); ajax.onreadystatechange=function(){ if(ajax.readyState==4 &amp;&amp; (ajax.status==200||ajax.status==0)){ eval('var data = ' + ajax.responseText + ';'); var theResults = data.dataset; var theHtml = ''; for(var i=0;i&lt;theResults.length;i++){ if (theResults[i].system_1 == 1) { var theHtml = theHtml + '&lt;input name="system_1" type="checkbox" id="system_1" value="1" checked="checked" /&gt;MS Excel'; } else { var theHtml = theHtml + '&lt;input name="system_1" type="checkbox" id="system_1" value="0" /&gt;MS Excel'; } if (theResults[i].system_2 == 1) { var theHtml = theHtml + '&lt;BR&gt;&lt;input name="system_2" type="checkbox" id="system_2" value="1" checked="checked" /&gt;MYOB'; } else { var theHtml = theHtml + '&lt;BR&gt;&lt;input name="system_2" type="checkbox" id="system_2" value="0" /&gt;MYOB'; } if (theResults[i].system_3 == 1) { var theHtml = theHtml + '&lt;BR&gt;&lt;input name="system_3" type="checkbox" id="system_3" value="1" checked="checked" /&gt;Quickbooks'; } else { var theHtml = theHtml + '&lt;BR&gt;&lt;input name="system_3" type="checkbox" id="system_3" value="0" /&gt;Quickbooks'; } if (theResults[i].system_4 == 1) { var theHtml = theHtml + '&lt;BR&gt;&lt;input name="system_4" type="checkbox" id="system_4" value="1" checked="checked" /&gt;Xero'; } else { var theHtml = theHtml + '&lt;BR&gt;&lt;input name="system_4" type="checkbox" id="system_4" value="0" /&gt;Xero'; } if (theResults[i].system_5 == 1) { var theHtml = theHtml + '&lt;BR&gt;&lt;input name="system_5" type="checkbox" id="system_5" value="1" checked="checked" /&gt;SAP'; } else { var theHtml = theHtml + '&lt;BR&gt;&lt;input name="system_5" type="checkbox" id="system_5" value="0" /&gt;SAP'; } if (theResults[i].system_6 == 1) { var theHtml = theHtml + '&lt;BR&gt;&lt;input name="system_6" type="checkbox" id="system_6" value="1" checked="checked" /&gt;Navision'; } else { var theHtml = theHtml + '&lt;BR&gt;&lt;input name="system_6" type="checkbox" id="system_6" value="0" /&gt;Navision'; } if (theResults[i].system_7 == 1) { var theHtml = theHtml + '&lt;BR&gt;&lt;input name="system_7" type="checkbox" id="system_7" value="1" checked="checked" /&gt;Others'; } else { var theHtml = theHtml + '&lt;BR&gt;&lt;input name="system_7" type="checkbox" id="system_7" value="0" /&gt;Others'; } } } document.getElementById('main').innerHTML = theHtml; } } function updateSystem (form) { var system_1 = form.system_1.value; var system_2 = form.system_2.value; var system_3 = form.system_3.value; var system_4 = form.system_4.value; var system_5 = form.system_5.value; var system_6 = form.system_6.value; var system_7 = form.system_7.value; var ajax = new XMLHttpRequest(); ajax.open("GET","domain/system_setup_update.php?user_id="+sessionStorage.getItem('myId')+"&amp;system_1="+system_1+"&amp;system_2="+system_2+"&amp;system_3="+system_3+"&amp;system_4="+system_4+"&amp;system_5="+system_5+"&amp;system_6="+system_6+"&amp;system_7="+system_7+"",true); ajax.send(); ajax.onreadystatechange=function(){ if(ajax.readyState==4 &amp;&amp; (ajax.status==200||ajax.status==0)){ eval('var data = ' + ajax.responseText + ';'); var theResults = data.dataset; for(var i=0;i&lt;theResults.length;i++){ var theRefresh = window.location = "system_setup.html"; } document.getElementById('refresh').innerHTML = theRefresh; } } } document.addEventListener("deviceready", appReady, false); &lt;/script&gt; &lt;style type="text/css"&gt; .line {padding-bottom:2px;} .title {float: left; width: 80px;} .content {margin-left: 90px; } .clear{clear:both;} &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;System Used&lt;/h1&gt; &lt;FORM NAME="myform" ACTION="" METHOD="GET"&gt; &lt;div id="main"&gt; &lt;/div&gt; &lt;INPUT TYPE="button" NAME="button" Value="Update" onClick="updateSystem(this.form)"&gt; &lt;/FORM&gt; &lt;div id="refresh"&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Thanks in advanced!!</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