0

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();
?>
theduck
  • 2,589
  • 13
  • 17
  • 23
Deepak
  • 13
  • 7
  • What are you expecting to happen? ... and what isn't? As far as I can tell, you should just get an alert that says *"checked"* - is that not happening? Or is that not what you want to happen? What's the actual question here? – CD001 Oct 24 '19 at 15:18
  • [Obligatory SQL injection warning](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – CD001 Oct 24 '19 at 15:19
  • `$.post('http://localhost/test/checkmail.php'` You cannot use localhost here. Use the local ip adress of the pc where the server is running on. – blackapps Oct 24 '19 at 15:25

1 Answers1

0

Instead of writing http://localhost/ try to write http://127.0.0.1/

Permsubs
  • 23
  • 4
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33370442) – Vojin Purić Dec 13 '22 at 15:12