Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-04-2010, 12:33 AM   PM User | #1
ttmtake
New to the CF scene

 
Join Date: Jun 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ttmtake is an unknown quantity at this point
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
ttmtake is offline   Reply With Quote
Old 07-12-2010, 01:59 PM   PM User | #2
InterbredMonkey
New Coder

 
Join Date: Jan 2010
Location: In a dump
Posts: 64
Thanks: 9
Thanked 2 Times in 2 Posts
InterbredMonkey is an unknown quantity at this point
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;
}
InterbredMonkey is offline   Reply With Quote
Old 07-12-2010, 06:45 PM   PM User | #3
ttmtake
New to the CF scene

 
Join Date: Jun 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ttmtake is an unknown quantity at this point
Tnx, will try.
ttmtake is offline   Reply With Quote
Old 07-15-2010, 07:06 PM   PM User | #4
ttmtake
New to the CF scene

 
Join Date: Jun 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
ttmtake is an unknown quantity at this point
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.
ttmtake is offline   Reply With Quote
Old 06-06-2011, 01:17 PM   PM User | #5
xraymutation
New to the CF scene

 
Join Date: Jun 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
xraymutation is an unknown quantity at this point
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
xraymutation is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:58 AM.


Advertisement
Log in to turn off these ads.