I am trying to query a php file stored in a local host (MAMP) from javascript stored in local machine. I am using this method because my end application will be an android app that is made using cordova which stores html, JS and CSS files on mobile and I need a function in JS to query a server. Below is code:
Javascript:
function onclickagree()
{
var emailid = $('#emailad').val();
$.post('http://localhost/test/checkmail.php',{postemail:emailid},
function(data)
{
alert("checked");
});
}
php file:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "userinfo";
$emailidval = $_POST['postemail'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM userregistration WHERE email = '$emailidval'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "email exists";
}
else
{
echo "email doesn't exists";
}
$conn->close();
?>