Monday 13 April 2015

PASSING- FORM- DATA -FORM -ANDROID -APPS -TO -SERVER (CORDOVA & PHONEGAP TECHNOLOGY)

(1)html file & ajax code on  Andorid apps  
<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 = "http://test.itexamtime.com/apps/my_parse_file.php";
    var fn = document.getElementById("name").value;
    var en = document.getElementById("email").value;
var pn = document.getElementById("phone").value;
var pd = document.getElementById("password").value;
var cpd = document.getElementById("password").value;

    var vars = "name="+fn+"&email="+en+"&phone="+pn+"&password="+pd+"&cpassword="+cpd;
    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>

Name:<input id="name" name="name" type="text"><br><br>


EMAIL:<input id="email" name="email" type="text"><br><br>


PHONE<input id="phone"    name="phone"     type="text"><br><br>


PASSWORD<input id="password"    name="password"     type="text"><br><br>
PASSWORD<input id="cpassword"    name="cpassword"     type="text"><br><br>


<input name="myBtn" type="submit" value="Submit Data" onclick="ajax_post();"> <br><br>

<div id="status"></div>
</body>
</html>

(2) see following code for  php file which is on server:


<?php

$name=$_POST['name'];

$email=$_POST['email'];

$phone=$_POST['phone'];

$password=$_POST['password'];

$cpassword=$_POST['cpassword'];



$con=mysql_connect("localhost","vdigital_ittest","vdigital_itexamtime");

mysql_select_db("vdigital_itexam_test",$con) or die(mysql_error());

if(!$con)

{


echo "databse not connected";

}
$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

{
if($password!==$cpassword)

{

echo "password did not match";

}
else{


$sql1="insert into studentrecord(name,email,phone,password)values('$name','$email','$phone','$password')";

mysql_query($sql1);

echo "success registered";

}

}

?>

No comments:

Post a Comment