Shakeel bhai mene user se comments name or email address lene ke liye ye banaya hai ismein aik problem jub mein validate kerta hoon ke user na kitne words type kiye agar condition true hoti hai to ye sirf ture he show kerta hai or agar false to error show kerdeta hai.
Ye form hai isse comment.php ke name se save karein.
HTML Code:
<html>
<head><title>Comment Form</title></head>
<style type="text/css">
body {
border: 2% solid #dddddd;
margin: 4%;
padding: 4%;
}
.button {
border:1px inset #dddddd;
}
</style>
<body>
<center><form method="post" action="commentform.php">
<table border="1" cellpadding="2" cellspacing="0" width="75%" style="border-collapse:collapse;border-bottom:1px solid #dddddd;border-left:2px solid #ccc;border-right:2px solid #ccc;border-top:1px solid #dddddd;">
<tr><td><b>Name:</b></td><td><input type="text" name="name"/></td></tr>
<tr><td><b>Email:</b></td><td><input type="text" name="email"/></td></tr>
<tr><td><b>Country:</b></td><td><select name="country"><option>Pakistan</option>
<option>China</option><option>UK</option></select></td></tr>
<tr><td><b>Commetns:</b></td><td><textarea name="comments"></textarea></td></tr>
<tr><td> </td><td><input type="submit" value="Submit" class="button"/> <input type="reset" value="Reset" class="button"/></td></tr>
</table>
</form></center>
</body>
</html>
Or php code ise commentform.php ke name se save karein.
PHP Code:
<html>
<head><title>Comment Form</title></head>
<body>
<center>
<?php
error_reporting(E_ALL ^ E_NOTICE);
function cleanstr($string){
$cstr = trim($string);
$cstr = addslashes($cstr);
$cstr = htmlspecialchars($cstr);
return $cstr;
}
function stringlen($lenght){
$len = cleanstr($lenght);
$len = strlen($len);
if($len < 10 || $len > 40){
echo "Your comments must in between 10 and 40 words";
return false;
}
return true;
}
function emptyfields($empty){
if(!is_array($empty)){
echo "The argument must be in array";
return false;
}
foreach($empty as $keys => $values){
$values = cleanstr($values);
if(empty($values)){
echo "<br />" . ucwords($keys) . "</br /> must not be empty";
return false;
}
}
return true;
}
if(!emptyfields($_POST)){
exit();
}
$name = cleanstr($_POST['name']);
$email = cleanstr($_POST['email']);
$country = $_POST['country'];
$comments = stringlen($_POST['comments']);
echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"0\" width=\"75%\" style=\"border-collapse:collapse;border-bottom:1px solid #dddddd;border-left:2px solid #ccc;border-right:2px solid #ccc;border-top:1px solid #dddddd;\">";
echo "<tr><td>".$name."</td></tr>";
echo "<tr><td>".$email."</td></tr>";
echo "<tr><td>".$country."</td></tr>";
echo "<tr><td>".$comments."</td></tr>";
echo "</table>";
?>
</center>
</body>
</html>
Bookmarks