Slip 13 - B) Create web Application that contains Voters details and check proper validation for (name, age, and nationality), as Name should be in upper case letters only, Age should not be less than 18 yrs and Nationality should be Indian.(use HTML-AJAX-PHP).

Solution:


Html File:

<html>
<head>
<h2> User information</h2>
<body>
<script type="text/javascript">
function validate()
{
var xmlhttp;
if(window.XMLHttpRequest)
xmlhttp= new XMLHttpRequest();
else
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    name=f1.txt1.value;
    age=f1.txt2.value;
    nationality=f1.txt3.value;
    xmlhttp.open("GET","slip13-p2-q2.php?            txt1="+name+"&txt2="+age+"&txt3="+nationality,false);
    xmlhttp.send();
    document.getElementById("result").innerHTML=xmlhttp.responseText;
}
</script>
<form name="f1">
Enter Name:
<input type="text" name="txt1" /><br><br>
Enter Age:
<input type="text" name="txt2" /><br><br>
Enter Nationality:
<input type="text" name="txt3" /><br><br>
<input type="button" value="submit" onclick="validate()">
<div id="result"></div>
</form>
</body>
</html>

Php File:

<?php
$name=$_GET['txt1'];
$age=$_GET['txt2'];
$nationality=$_GET['txt3'];
$hint="";
if($name==="")
$hint="<br>Enter name";
else if(!preg_match("/^([A-Z\sA-Z']+)$/",$name))
$hint='<br>Please enter valid name.';
if($age==="")
$hint.="<br>Enter age";
else if($age<18)
{
$hint.='<br>Please enter valid age';
}

if($nationality==="")
$hint.="<br>Enter Nationality";
else
if($nationality!="Indian")
$hint.='<br>Please enter valid nationality';
echo $hint===""?"<br>Valid input no suggestion":$hint;
?> 

Post a Comment

0 Comments