I am trying to get a html page with jquery ajax to connect to an online database and return true or false if a query returns true or false.
Here is the code for the index.html file (This is running on my local pc in wamp)
The Query/Ajax Code:
<script type="text/javascript">
function submitForm() {
if(!$("#username").val()){
alert("username field cannot be empty.");
return false;
}
if(!$("#password").val()){
alert("password field cannot be empty.");
return false;
}
$.ajax({type:'POST',
url: 'http://mywebsite.com/test/index.php',
data:$('#ContactForm').serialize(), success: function(response) {
$('#ContactForm').find('.form_result').html(response);
}});
return false;
}
</script>
And the Form...
<form id="ContactForm" onsubmit="return submitForm();">
Username: <input id="username" type="text" name="username" value="" /><br />
Password: <input id="password" type="text" name="password" value="" /><br />
<br />
<input type="submit" name="submit" value="Submit" /><br />
<div class="form_result"></div>
</form>
Now, here is the code on the server side...This file I''ve uploaded to the server where the database is resident:
<?php
if(isset($_POST['username'])) {
$username = $_POST['username'];
$password = md5($_POST['password']);
$con=mysqli_connect("localhost","dbuser","pass","myDatabase"); //Server Online
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM tblusers WHERE username='".$username."'");
if ($result->num_rows) {
echo "right login !!";
} else {
echo 'wrong login';
}
mysqli_close($con);
}
?>
The problem is that nothing is returned on the html page (callback) ...
In other words.. this line:
$('#ContactForm').find('.form_result').html(response);
is not being called.
I've checked the console log and it returns 200 OK and the posts is actually submitted BUT nothing is returned to the page.
Any ideas what could be going wrong?
.html(response.responseText);