Monday, May 19, 2008

Checkbox array problem with Javascript and PHP

Check this solution...
Here we can use the checkbox array.... no problem with both php get data and javascipt validation.


<?php
if(isset($_REQUEST['lang']))
{
$lang=$_REQUEST['lang'];
foreach ($lang as $language)
{
echo "$language is checked <br />";
}
echo "Selected Array: ";
print_r($lang);
}
?>
<html>
<head>
<title>TEST</title>
<script language="javascript">
function chkThis()
{
var chkList = document.chkForm['lang[]'];
for(var i = 0 ; i < chkList.length ; i++)
{
if(chkList[i].checked)
alert(chkList[i].value);
}
}
</script>
</head>
<body>
<form method="post" action="#" name="chkForm" onsubmit="return chkThis();">
Please choose language:<br />
<input type="checkbox" name="lang[]" value="JAVA">JAVA><br />
<input type="checkbox" name="lang[]" value="VB">VB<br />
<input type="checkbox" name="lang[]" value="PHP">PHP<br />
<input type="submit" value="submit" name="submit">
</form>
</body>
</html>

No comments: