Checkboxes in JavaScript and PHP
Posted: Sun Nov 27, 2005 9:32 am
Hi all,
I am having a problem with using javascript and php in the same project. I have simplified my code to make my problem easier to understand. Basically my probelm is: I have a form in HTML with a series of check boxes, that are manipulated in javascript. I do this by creating javascript funtions for the checkboxes, and calling them using "onClick=(apress(document.form1.boxes)" in my check box tag. This works fine, until I want to send these off to my PHP script in the $_POST array. I figured out how to get the values in the PHP by adding "[]" after name of each checkbox, but once I do this I am unable to use the javascript functions even when I change it to "onClick=(apress(document.form1.boxes[])"
My HTML and JavaScript:
my PHP:
Thanks for reading, and please let me know if you can help,
Mur
I am having a problem with using javascript and php in the same project. I have simplified my code to make my problem easier to understand. Basically my probelm is: I have a form in HTML with a series of check boxes, that are manipulated in javascript. I do this by creating javascript funtions for the checkboxes, and calling them using "onClick=(apress(document.form1.boxes)" in my check box tag. This works fine, until I want to send these off to my PHP script in the $_POST array. I figured out how to get the values in the PHP by adding "[]" after name of each checkbox, but once I do this I am unable to use the javascript functions even when I change it to "onClick=(apress(document.form1.boxes[])"
My HTML and JavaScript:
Code: Select all
<html>
<form name="form1" method="post" action="mailer.php">
<p>
<input type="checkbox" name="boxes[]" value="a" onClick="apress(document.form1.boxes[])">
a
</p>
<p> <input type="checkbox" name="boxes[]" value="b">
b
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</html>
<script language="javascript">
function apress(field) {
if(field[0].checked == true) {
field[1].checked = false;
alert();
}
}
</script>Code: Select all
<?php
$checkboxes = $_POST['boxes'];
foreach($checkboxes as $box) {
$message = $message . $box . "\n";
}
echo $message;
?>Mur