Page 1 of 1

Checkboxes

Posted: Mon Jul 16, 2007 4:10 am
by legend986
Inorder to use checkboxes in php, i use something like the following:

Code: Select all

<input type="checkbox" name="selection[]" value="<?php echo $row[$i] ?>" /> Check box 1
<input type="checkbox" name="selection[]" value="<?php echo $row[$i] ?>" /> Check box 2
<input type="checkbox" name="selection[]" value="<?php echo $row[$i] ?>" /> Check box 3
<input type="checkbox" name="selection[]" value="<?php echo $row[$i] ?>" /> Check box 4
<input type="checkbox" name="selection[]" value="<?php echo $row[$i] ?>" /> Check box 5
It is perfectly alright if I want to manipulate data based on this selection[] array but how would I send this as a variable to another php script using Ajax.

in Ajax i use something like:

Code: Select all

var e_id = document.getElementById('selection').value;
	var queryString = "?e_id=" + e_id;
	ajaxRequest.open("GET", "cc/fs.php" + queryString, true);
	ajaxRequest.send(null);
But it says selection has no properties in the Javascript Error Console... Is there a workaround for this or am i using everything in an incorrect way?

PS: This script contains both PHP and Ajax so I was confused where to post it. I'm sorry if this is the wrong forum...

Posted: Mon Jul 16, 2007 4:47 am
by volka
You want the browser to prepare a query to a http server. That's client-side. Your browser doesn't care, doesn't even know that it got a html document that was created by php. And php doesn't care whether ajax was involved or not.
try

Code: Select all

<html>
	<head>
		<title>...</title>
		<script type="text/javascript">
			function foo() {
				querystring = "bar=" + document.getElementById("bar").value;
				nodeset = document.getElementsByName("selection[]");
				for(i=0; i<nodeset.length; i++) {
					querystring += "&" + escape(nodeset[i].name) + "=" + escape(nodeset[i].value);
				}
				document.getElementById("output").value = querystring;
			}
		</script>
	</head>
	<body>
		<input type="texts" name="bar" id="bar" value="xyz" /><br />
		<input type="checkbox" name="selection[]" value="1" /> Check box 1<br />
		<input type="checkbox" name="selection[]" value="2" /> Check box 2<br />
		<input type="checkbox" name="selection[]" value="3" /> Check box 3<br />
		<input type="checkbox" name="selection[]" value="4" /> Check box 4<br />
		<input type="checkbox" name="selection[]" value="5" /> Check box 5<br />
		<button onclick="foo()">click</button><br />
		<input type="text" id="output" size="60" />
	</body>
</html>