Wednesday, 27 December 2017

AWT-Slip8

1 )Write a script to keep track of number of times the web page has been accessed

(use $_COOKIE).
coo8.html
<html>
<body>
<form method=post action=coo8.php>
Enter name<input type=text name=nm><p>
<input type="submit" value="click me">
</form>
</body>
</html>

 

coo8.php

<?php
$name = 'visitCount';
if(!isset($_COOKIE[$name])){
 $_COOKIE[$name] = 0;
}
$_COOKIE[$name] = 1+(int) max(0,$_COOKIE[$name]);
$result = setcookie($name,$_COOKIE[$name]);
if(!$result) {
 throw new RuntimeException("Failed to set cookie \"$name\"");
}
echo "Hello <b>".$_POST['nm']."</b><br>";
echo"<br/> You have accessed the page <b>".$_COOKIE[$name]."</b> times<br>";
echo"<br/><a href='coo8.html'>coo8.html</a>";
?>

 2)Write Ajax program to carry out validation for ausername entered in textbox. If the textbox is blank, print ‘Enter username’.If the number of characters is less than three, print’ Username is too short’.If value entered is appropriate the print ‘Valid username’.  

Slip8.html


<html>

<head>

<script type="text/javascript">

function valu(str)

{

var ob1=false;

ob1=new XMLHttpRequest();

if(str=="")

document.getElementById("a").innerHTML="Username Should not be Empty";                                   else if(str.length<=3)

document.getElementById('a').innerHTML="lenght is too short";      

else document.getElementById('a').innerHTML="valid username";

}

</script>

</head>

<body>

<form>

<table align="center">

<tr>

<td><b>Enter Username : </b></td>

<td><input type=text name=u_name id=u_name></td>

</tr>

<tr>

<td></td>

<td><font color="red" size=3><span id=a></span></font></td>

</tr>

<tr>

<th colspan="2"><input type=button value=submit onclick="valu(form.u_name.value)"></th></tr>

</table>

</form>

</body>

</html>