Tuesday, 4 August 2015

Ajax_example_for posting_data_to_php & databse

07:08

Note: create table studentrecord & insert email id & check :

(1)WRITE CODE  FOR  FORM.HTML:


<html>
<head>
<script>
function ajax_post(){
    // Create our XMLHttpRequest object
    var hr = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "my_parse_file.php";
    var fn = document.getElementById("first_name").value;
    var ln = document.getElementById("last_name").value;

var gn = document.getElementById("email").value;


    var vars = "firstname="+fn+"&lastname="+ln+"&email="+gn;

    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
    if(hr.readyState == 4 && hr.status == 200) {
    var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
    }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
    document.getElementById("status").innerHTML = "processing...";
}
</script>
</head>
<body>
<h2>Ajax Post to PHP and Get Return Data</h2>
First Name: <input id="first_name" name="first_name" type="text">  <br><br>
Last Name: <input id="last_name" name="last_name" type="text"> <br><br>
<input id="email"    name="email"     type="text"> <br><br>



<input name="myBtn" type="submit" value="Submit Data" onclick="ajax_post();"> <br><br>
<div id="status"></div>
</body>
</html>


(2)WRITE CODE FOR PHP FILE  "my_parse_file.php" :


<?php

$email= $_POST['email'];

$con=mysql_connect("localhost","root","");

mysql_select_db("itexam",$con);

$sql="select * from  studentrecord where email='$email'";

$result=mysql_query($sql);

$num=mysql_num_rows($result);

if($num>0)

{

echo "email id is already used";

}

else

{


echo "email id  is available";

}

?>

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top