I have a simple jQuery AJAX request that submits user input from a text box to a PHP file. Here's the code for it:
$.ajax({
url: url,
type: type,
data: data,
dataType: 'json',
success: function(response){
console.log(response);
}
});
The PHP file basically logs the user in. Everything worked fine until I added "dataType: 'json'
" to my AJAX request. Now, whenever I click my submit button, nothing logs. Here is my PHP file:
<?php
include 'dbcon.php';
if ( isset( $_POST['text_login_username'], $_POST['text_login_password'] ) ) {
$loginResult = array();
$dbcon = getConnection();
$userName = mysqli_real_escape_string( $dbcon, $_POST['text_login_username'] );
$password = mysqli_real_escape_string( $dbcon, $_POST['text_login_password'] );
$loginQuery = "SELECT * FROM userData WHERE userName='$userName' AND userPassword='$password'";
$queryResult = mysqli_query( $dbcon, $loginQuery );
$legalRows = mysqli_num_rows( $result );
if ( $legalRows == 1 ) {
$loginResult['allClear']=0;
} else {
$loginResult['allClear']=1;
}
echo json_encode( $loginResult );
}
?>
AJAX file
$(document).ready(function(){
$('form.loginSubmit').on('submit',function(){
var that = $(this),
url=that.attr('action'),
type=that.attr('method'),
data={};
that.find('[name]').each(function(index,value){
var that=$(this),
name=that.attr('name');
value=that.val();
data[name]=value;
});
$.ajax({
url: url,
type: type,
data: data,
contenType:'application/json; charset=utf-8',
dataType:'json',
success: function(response){
console.log(response);
},
error: function(error)
{
console.log("test");
}
});
return false;
});
});
I can assure that the proper links to files, posts, etc. are set up properly, because this works until I try to send out the json_encode
variable. Any help would be much appreciated!
Thanks!
~Carpetfizz
UPDATE: I added an error:
setting to my AJAX call, and it runs whenever I submit.
UPDATE: Check out my answer. This was the solution for me.