I have a simple script that keeps resulting in error, even though the PHP script is run successfully.
PHP
require('/connect.php'); //Has my $link variable
if(isset($_POST['pass'])){
$user = $_POST['user'];
$oldpass = md5(urldecode($_POST['op']));
$newpass = md5(urldecode($_POST['np']));
$q = "SELECT * FROM `nht_users` WHERE usr = '".$user."' AND pass = '".$oldpass."'";
$r = mysqli_query($link,$q);
$n = mysqli_num_rows($r);
if( $n > 0){
$u = "UPDATE `nht_users` SET pass = '".$newpass."' WHERE usr = '".$user."'";
mysqli_query($link,$u);
}
else{
echo json_encode('FALSE');
}
}
JQuery
$('#pass-submit').click(function() {
var npp = $('#npp').val(),
np = $('#np').val(),
data = $('#pass-form').serialize(),
url = 'http://www.<?php echo DOMAIN; ?>/functions.php';
if (npp == np) {
$('#test').html(url);
$.ajax({
type: 'POST',
url: url,
data: 'pass=true&'+data,
success: function(data){
$('#pass-form').slideUp(800);
$('.pass-true').fadeIn(800);
},
error: function(){
$('.pass-false').fadeIn(800);
}
});
}
else {
$('.pass-check').fadeIn(800);
}
});
To catch the error
error: function(xhr, textStatus, errorThrown) {
alert(xhr.responseText);
}
To get a better idea of what is happening in your response, you can try to use the network tab in Chrome (hit F12) and run your process. You should see your ajax request and response come through. Internet Explorer also has a similar option, but you will need to tell it to explicitly watch the network. One other option for tracing this type of issue is to install a desktop application like Fiddler2 (http://fiddler2.com/).
I have used all 3 of these to varying levels to hunt down why my AJAX requests are not running as expected.