here isthe complete code ...please help me in validating rollnum field
PHP Code:
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("tutorial", $con);
if($_REQUEST["submit"]=="submit")
{
$sql="INSERT INTO friends (name, qualification, rollnum)
VALUES
('$_POST[name]','$_POST[qualification]','$_POST[rollnum]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
}
mysql_close($con);
?>
<html>
<head>
<script type="text/javascript">
function validateform()
{
valid=true;
var Numbers = '0123456789';
var x=document.getElementById("nameid").value;
if(x==''){alert("name should not be empty");return false;}
var y=document.getElementById("qualificationid").value;
if(y==''){alert("qualification should not be empty");return false;}
var z=document.getElementById("rollnumid").value;
if(z==''){alert("rollnum should not be empty");return false;}
if(isNaN(z))
alert("Characters are not allowed in the Phone Number");
return false;
}
</script>
<body>
<div style="position:absolute;top:100px;left:60px;">
<h1 align="center"><font size=5 color="red">submit details to the table</font></h1>
<form name=form action="form.php" method="post">
<table border=2 cellspacing=0 cellpadding=0>
<tr><td>name</td><td><input type="text" name="name" id="nameid" /></td></tr></br>
<tr><td>qualification</td><td> <input type="text" name="qualification" id="qualificationid" /></td></tr></br>
<tr><td>rollnum</td><td><input type="text" name="rollnum" id="rollnumid"/></td></tr></br>
</table></br>
<input type="submit" name="submit" value="submit" onclick="return validateform()">
</form></div>
<div style="position:absolute;bottom:60px;left:60px;">
<?php
echo "the added record is $_REQUEST[name],$_REQUEST[qualification],$_REQUEST[rollnum] ";
?></div>
<div style="position:absolute;top:60px;left:600px;">
<html>
<head>friends table</head>
<title>db</title>
<body><br>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
echo "the table from the database is as follows";
$db_selected = mysql_select_db("tutorial",$con);
$string="select * from friends";
$result=mysql_query($string,$con);
?>
<table align="center" border="4" cellpadding="5" cellspacing="5" width="50%">
<tr>
<th>name</th>
<th>qualification</th>
<th>rollnum</th>
</tr>
<?php
while($row=mysql_fetch_array($result))
{
echo "<tr><td align='center'>$row[name]</td><td align='center'>$row[qualification]</td><td align='center'>$row[rollnum]</td></tr>";
}
?>
</table></div>
</body>
</html>