+ Reply to Thread
Results 1 to 5 of 5

Thread: Checkboxes and AJAX

  1. #1
    playminigames is offline x10 Sophmore playminigames is an unknown quantity at this point
    Join Date
    Jul 2009
    Location
    earth
    Posts
    216

    Checkboxes and AJAX

    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!

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Checkboxes and AJAX

    Could you post the code you have, both server side and client side?

    Usually checkboxes send nothing if they are unchecked.
    Nothing is always absolutely so.

  3. #3
    playminigames is offline x10 Sophmore playminigames is an unknown quantity at this point
    Join Date
    Jul 2009
    Location
    earth
    Posts
    216

    Re: Checkboxes and AJAX

    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;
    var imen = document.getElementById('interestedmen').value;
    var iwomen = document.getElementById('interestedwomen').value;
    
    
      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 +"&imen="+ imen +"&iwomen="+ iwomen,true);
    xmlhttp.send(null);
    }
    and then the checkboxes
    HTML Code:
              <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

  4. #4
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Checkboxes and AJAX

    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.
    Nothing is always absolutely so.

  5. #5
    playminigames is offline x10 Sophmore playminigames is an unknown quantity at this point
    Join Date
    Jul 2009
    Location
    earth
    Posts
    216

    Re: Checkboxes and AJAX

    Thanks it works perfectly, im wondering why i wasnt able to find that on google, thanks! Rep added
    Last edited by playminigames; 10-28-2009 at 05:44 PM.

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers