Checkboxes and AJAX

playminigames

New Member
Messages
216
Reaction score
6
Points
0
Ok so ive been trying to make a part of my site where you can edit your preferences, and i need to have 2 different checkboxes, and im making it all update with ajax, but the thing is whenever is submits it sends the value of it even if it is not selected, i dont have much experience with javascript and ajax so i was hoping some of you could help me out here.Also all of this is being submitted to a mysql database

Thanks!
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Could you post the code you have, both server side and client side?

Usually checkboxes send nothing if they are unchecked.
 

playminigames

New Member
Messages
216
Reaction score
6
Points
0
yea sure here is the code to get all of the stuff

Code:
function updateinfo()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {
	  		var ajaxDisplay = document.getElementById('hiddendiv');
			ajaxDisplay.innerHTML = xmlhttp.responseText;
  }
}

var sex = document.getElementById('sex').value;
var relationship = document.getElementById('relationship').value;
var bday_day = document.getElementById('day').value;
var bday_month = document.getElementById('month').value;
var bday_year = document.getElementById('year').value;
var hometown = document.getElementById('hometown').value;
var neighborhood = document.getElementById('neighborhood').value;
[B]var imen = document.getElementById('interestedmen').value;
var iwomen = document.getElementById('interestedwomen').value;[/B]


  xmlhttp.open("GET","http://www.playminigames.co.cc/ajax/updateprofileinfo.php?sex=" + sex + "&bday_day="+ bday_day +"&relationship="+ relationship +"&hometown="+ hometown +"&neighborhood="+ neighborhood +"&bday_month="+ bday_month +"&bday_year="+ bday_year [B]+"&imen="+ imen +"&iwomen="+ iwomen[/B],true);
xmlhttp.send(null);
}
and then the checkboxes
HTML:
          <input type="checkbox" id="interestedmen" value="yes">Men
          <br>
          <input type="checkbox" id="interestedwomen" value="yes">Women

everything else is fine, its just the checkboxes, if it doesnt work, i can always think of a different way to do it, but i would like it this way the most.

Thanks!

o and i cant really let you try it, because its all under development, and when i look in firebug it show that both values are "yes" when unchecked
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Quickest way ....

Code:
var imen = document.getElementById('interestedmen').checked;
var iwomen = document.getElementById('interestedwomen').checked;

Gives 'true' or 'false' which you can be sent back to the server.

If you submit a form normally, a checkbox value is sent only if it is checked. It's value does not change when it is checked or unchecked. Just whether or not it is sent with the rest of the form data.
 

playminigames

New Member
Messages
216
Reaction score
6
Points
0
Thanks it works perfectly, im wondering why i wasnt able to find that on google, thanks! Rep added
 
Last edited:
Top