Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ajax, sending checkboxes

    I have one problem with sending checkbox values, i have 7 checkboxes and when i send it, ajax send all 7,not just checked,here is my code:

    AJAX CODE:
    Code:
    //Browser Support Code
    function ajaxPoll(){
    	var ajaxRequest;  // The variable that makes Ajax possible!
    	
    	try{
    		// Opera 8.0+, Firefox, Safari
    		ajaxRequest = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Your browser broke!");
    				return false;
    			}
    		}
    	}
    	// Create a function that will receive data sent from the server
    	ajaxRequest.onreadystatechange = function(){
    		if(ajaxRequest.readyState == 4){
    			var ajaxDisplay = document.getElementById('poll_ajax');
    			ajaxDisplay.innerHTML = ajaxRequest.responseText;
    
    		}
    	}
    	var a = document.getElementById('a').value;
    	var b = document.getElementById('b').value;
    	var c = document.getElementById('c').value;
    	var d = document.getElementById('d').value;
    	var e = document.getElementById('e').value;
    	var f = document.getElementById('f').value;
    	var g = document.getElementById('g').value;
    
    	var queryString = "?a=" + a + "&b=" + b + "&c=" + c + "&d=" + d + "&e=" + e + "&f=" + f + "&g=" + g;
    	ajaxRequest.open("GET", "/scripts/pollAjax.php" + queryString, true);
    	ajaxRequest.send(null); 
    }

    HTML CODE:

    Code:
    <form method="POST">
    <div class="sur_box"><span>1</span><spanr><input name="a" type="checkbox" class="styled" id="a" value="1" /></spanr></div>
    <div class="sur_box"><span>2</span><spanr><input name="b" type="checkbox" class="styled" id="b" value="2" /></spanr></div>
    <div class="sur_box"><span>3</span><spanr><input name="c" type="checkbox" class="styled" id="c" value="3" /></spanr></div>
    <div class="sur_box"><span>4</span><spanr><input name="d" type="checkbox" class="styled" id="d" value="4" /></spanr></div>
    <div class="sur_box"><span>5</span><spanr><input name="e" type="checkbox" class="styled" id="e" value="5" /></spanr></div>
    <div class="sur_box"><span>6</span><spanr><input name="f" type="checkbox" class="styled" id="f" value="6" /></spanr></div>
    <div class="sur_box"><span>7</span><spanr><input name="g" type="checkbox" class="styled" id="g" value="7" /></spanr></div>
    <div id="poll_ajax">
    <input name="search" type="button" onclick="ajaxPoll();" class="submit" value="SEND" id="search" />
    </div>
    </form>
    PHP CODE:
    Code:
    <?php
    require_once('../config.php');
    if($_GET)
    {
    
    $a=$_GET["a"];
    $b=$_GET["b"];
    $c=$_GET["c"];
    $d=$_GET["d"];
    $e=$_GET["e"];
    $f=$_GET["f"];
    $g=$_GET["g"];
    
    $ip = $_SERVER['REMOTE_ADDR'];
    $date = date("F j, Y, g:i a");
    
    $tabla = mysql_query("SELECT * FROM `tb_votes` WHERE `ip`='$ip'");  
    $row = mysql_fetch_array($tabla);
    $numrows=mysql_num_rows($tabla);
    
    if($numrows=='0'){
    $queryz = sprintf("INSERT INTO `tb_votes` (`a`,`b`,`c`,`d`,`e`,`f`,`g`,`voted_on`,`ip`) VALUES('%s','%s','%s','%s','%s','%s','%s','%s','%s')",
    
    mysql_real_escape_string($a),
    mysql_real_escape_string($b),	
    mysql_real_escape_string($c),	
    mysql_real_escape_string($d),	
    mysql_real_escape_string($e),	
    mysql_real_escape_string($f),	
    mysql_real_escape_string($g),	
    mysql_real_escape_string($date),																mysql_real_escape_string($ip));
    mysql_query($queryz) or die(mysql_error());	
    }
    }
    
    ?>

    So i get added in databse all 7, but i need to add just checked ones

  • #2
    New Coder
    Join Date
    Jan 2010
    Location
    In a dump
    Posts
    64
    Thanks
    9
    Thanked 2 Times in 2 Posts
    Put a script line in which checks to see if the box is checked like:

    Code:
    if(document.getElementById('a').checked = "true"){
             var a = document.getElementById('a').value;
    }

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Tnx, will try.

  • #4
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have changed my ajax code with:

    Code:
    //Browser Support Code
    function ajaxPoll(){
    	var ajaxRequest;  // The variable that makes Ajax possible!
    	
    	try{
    		// Opera 8.0+, Firefox, Safari
    		ajaxRequest = new XMLHttpRequest();
    	} catch (e){
    		// Internet Explorer Browsers
    		try{
    			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch (e) {
    			try{
    				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch (e){
    				// Something went wrong
    				alert("Your browser broke!");
    				return false;
    			}
    		}
    	}
    	// Create a function that will receive data sent from the server
    	ajaxRequest.onreadystatechange = function(){
    		if(ajaxRequest.readyState == 4){
    			var ajaxDisplay = document.getElementById('poll_ajax');
    			ajaxDisplay.innerHTML = ajaxRequest.responseText;
    
    		}
    	}
        if(document.getElementById('a').checked = "true"){
    	var a = document.getElementById('a').value;
    }
        if(document.getElementById('b').checked = "true"){
    	var b = document.getElementById('b').value;
    }
        if(document.getElementById('c').checked = "true"){
    	var c = document.getElementById('c').value;
    }
        if(document.getElementById('d').checked = "true"){
    	var d = document.getElementById('d').value;
    }
        if(document.getElementById('e').checked = "true"){
    	var e = document.getElementById('e').value;
    }
        if(document.getElementById('f').checked = "true"){
    	var f = document.getElementById('f').value;
    }
        if(document.getElementById('g').checked = "true"){
    	var g = document.getElementById('g').value;
    }
    	var queryString = "?a=" + a + "&b=" + b + "&c=" + c + "&d=" + d + "&e=" + e + "&f=" + f + "&g=" + g;
    	ajaxRequest.open("GET", "/scripts/pollAjax.php" + queryString, true);
    	ajaxRequest.send(null); 
    }

    But its same again, its like all my chack boxes already checked, after submit it add all again.

  • #5
    New to the CF scene
    Join Date
    Jun 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    try that way:

    if(document.getElementById('a').checked == "true"){
    var a = document.getElementById('a').value;
    }


    the difference is '==' instead of '='

    it worked for me. tnx guys


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •