Here is an example for form validation with javascript
<html>
<head>
<title>From Validation</title>
<script language="javascript">
function chkForm(form)
{
if(form.cname.value == ""){ alert("Enter Name"); form.cname.focus(); return false; }
if(form.age.value == "0"){ alert("Select Age Level"); return false; }
}
</script>
</head>
<body>
<div align="center">
<form name="test" action="some_page.php" method="post" onsubmit="return chkForm(this);">
<table border="0" cellpadding="0" cellspacing="0" width="100%" id="table1">
<tr>
<td width="139">Name</td>
<td><input type="text" name="cname" size="20"></td>
</tr>
<tr>
<td width="139">Age</td>
<td><select size="1" name="age">
<option selected value="0">Select</option>
<option value="below 18">below 18</option>
<option value="18+">18+</option>
</select></td>
</tr>
<tr>
<td width="139"> </td>
<td><input type="submit" value="Submit" name="B1"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
Older posts
Radio button validation:
http://phpbeginners.blogspot.com/2008/06/radio-button-validation-in-javascript.html
checkbox validation:
http://phpbeginners.blogspot.com/2008/05/checkbox-validation-in-javascirpt.html
Showing posts with label checkbox. Show all posts
Showing posts with label checkbox. Show all posts
Thursday, September 25, 2008
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>
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>
Friday, May 16, 2008
Checkbox validation in Javascirpt
Try this:
Change checkbox values and click button.....
<form name="test">
<input type="checkbox" name="checkgroup" checked />
<input type="checkbox" name="checkgroup" />
<input type="checkbox" name="checkgroup" checked />
<input type="button" value="Button" name="B1" onclick="check_boxes();"></form>
<script type="text/javascript">
function check_boxes()
{
for (i=0; i<document.test.checkgroup.length; i++){
if (document.test.checkgroup[i].checked==true)
alert("Checkbox at index "+i+" is checked!")
}
}
</script>
From: http://www.javascriptkit.com/jsref/checkbox.shtml
Change checkbox values and click button.....
<form name="test">
<input type="checkbox" name="checkgroup" checked />
<input type="checkbox" name="checkgroup" />
<input type="checkbox" name="checkgroup" checked />
<input type="button" value="Button" name="B1" onclick="check_boxes();"></form>
<script type="text/javascript">
function check_boxes()
{
for (i=0; i<document.test.checkgroup.length; i++){
if (document.test.checkgroup[i].checked==true)
alert("Checkbox at index "+i+" is checked!")
}
}
</script>
From: http://www.javascriptkit.com/jsref/checkbox.shtml
Wednesday, April 16, 2008
Getting Checkbox Values in PHP
<?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>
</head>
<body>
<form method="post" action="#">
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>
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>
</head>
<body>
<form method="post" action="#">
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>
Subscribe to:
Posts (Atom)