Wednesday, 27 December 2017

AWT-Slip17

1) To create form that accept the user details. write php program to capitalize of first letter of each name and check user email address contain @ symbol.


<html>
<BODY>
<?php if($_SERVER['REQUEST_METHOD'] == 'GET') {?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<B>Name:</b><input type="text" name="fname">
<B>Email:</b><input type="text" name="email">
<input type=submit />
</form>
<?php 
}
elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
$fname = ucwords($_POST['fname']);
$email = $_POST['email'];
if((!empty($fname)) && (!empty($email)))
{
$e = explode('@',$email);
if(count($e)!=2)
echo "Wrong email id ... no @ or more @";
else
echo $fname.", We will contact you on your email id ($email)";
}
else echo "Fill all the information!!<br/>";
}
else
{
die ("Not able to process the script");
}
?>
</body>
</html>

2)Write a PHP program to accept username and password from the user. Validate it against the login table in the database. If there is a mismatch between username and password, then, display the error message as ―invalid user name and password; else display the message as ―Login successful‖ on the browser.     


loginform.html


<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<script src="scrip.js"></script>
</head>
<body>
<div id="mainform">
<div class="innerdiv">
<!-- Required Div Starts Here -->

<h2>Form Validation Using AJAX</h2>
<form action='#' id="myForm" method='post' name="myForm">

<h3>Fill Your Information!</h3>

<table>

<tr>
<td>Username</td>
<td><input id='username1' name='username' onblur="validate('username', this.value)" type='text'></td>
<td>
<div id='username'></div>
</td>
</tr>

<tr>
<td>Password</td>
<td><input id='password1' name='password' onblur="validate('password', this.value)" type='password'></td>
<td>
<div id='password'></div>
</td>
</tr>

<tr>
<td>Email</td>
<td><input id='email1' name='email' onblur="validate('email', this.value)" type='text'></td>
<td>
<div id='email'></div>
</td>
</tr>

<!--<tr>
<td>website</td>
<td><input id='website1' name='website' onblur="validate('website', this.value)" type='text'></td>
<td>
<div id='website'></div>
</td>
</tr>-->


</table>
<input onclick="checkForm()" type='button' value='Submit'>
</form>
</div>
</body>
</html>


script.js

function checkForm() {
// Fetching values from all input fields and storing them in variables.

var name = document.getElementById("username1").value;
var password = document.getElementById("password1").value;
var email = document.getElementById("email1").value;
//var website = document.getElementById("website1").value;


//Check input Fields Should not be blanks.
if (name == '' || password == '' || email == '') {
alert("Fill All Fields");
} else {
//Notifying error fields
var username1 = document.getElementById("username");
var password1 = document.getElementById("password");
var email1 = document.getElementById("email");
//var website1 = document.getElementById("website");
//Check All Values/Informations Filled by User are Valid Or Not.If All Fields Are invalid Then Generate alert.
if (username1.innerHTML == 'Must be 3+ letters' || password1.innerHTML == 'Password too short' || email1.innerHTML == 'Invalid email') {
alert("Fill Valid Information");
} else {
//Submit Form When All values are valid.
document.getElementById("myForm").submit();
}
}
}
// AJAX code to check input field values when onblur event triggerd.
function validate(field, query) {
var xmlhttp;
if (window.XMLHttpRequest) { // for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState != 4 && xmlhttp.status == 200) {
document.getElementById(field).innerHTML = "Validating..";
} else if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById(field).innerHTML = xmlhttp.responseText;
} else {
document.getElementById(field).innerHTML = "Error Occurred. <a href='index.php'>Reload Or Try Again</a> the page.";
}
}
xmlhttp.open("GET", "validation.php?field=" + field + "&query=" + query, false);
xmlhttp.send();
}

Another solution


slip_17.html
<?php
$con=@mysql_connect("localhost","root","") or die("I cannot connect");      //echo "connected successfuly";
$d=mysql_select_db("archana",$con);
$q=mysql_query("select * from login");

$emailid=$_REQUEST['emailid'];       //echo $emailid;
$password=$_REQUEST['password'];   

$n=0;
while($row=mysql_fetch_array($q))
{
if($row[0]==$emailid && $row[1]==$password)
 $n=1;
}

if($n==1)
echo "<center><h3>WELCOME USER</h3><center>";
else
echo "<i>Emailid or Password Missmatch</i>";

?>

slip_17.php
<?php
$con=@mysql_connect("localhost","root","") or die("I cannot connect");      //echo "connected successfuly";
$d=mysql_select_db("archana",$con);
$q=mysql_query("select * from login");

$emailid=$_REQUEST['emailid'];       //echo $emailid;
$password=$_REQUEST['password'];   

$n=0;
while($row=mysql_fetch_array($q))
{
if($row[0]==$emailid && $row[1]==$password)
 $n=1;
}

if($n==1)
echo "<center><h3>WELCOME USER</h3><center>";
else
echo "<i>Emailid or Password Missmatch</i>";


?>