I'm attempting to POST some data to a PHP file using the jQuery $.ajax function, and return a response. I've done similar things in the past, so I'm not sure where I'm slipping up.
I've attached DONE, FAIL, THEN, and ALWAYS promises to the $.ajax function, each with alerts in them, and no alerts are being fired. Here is the relevant JavaScript.
$.ajax({
url: "processingForm.php",
dataType: "json",
type: "POST",
data: { theFullData: JSON.stringify(postData) },
success: function (obj, textstatus) {
alert("Woo");
},
error: function (err) {
alert("Error");
}
})
.done(function (res) {
alert(res);
})
.fail(function (res) {
alert("fail");
})
.then(function (res) {
alert("then");
})
.always(function () {
alert("complete");
});
In my PHP file, the JSON data is parsed and eventually a string is inserted into the database. The string is written to the database properly about 75% of the time. I've never seen anything written to the LOG table, so maybe that is an issue for another post.
$outp = array();
$sql = (string)('INSERT INTO PBP_O (POST_DATA) values ("' . $str . '")');
mysqli_query($conn, $sql);
//ERROR Logging
$err = mysqli_error($conn);
$sql = (string)('INSERT INTO LOG (ERROR) values ("Hi")');
mysqli_query($conn, $sql);
if($err != ""){
mysqli_close($conn);
echo "Sorry";
}
else{
$sql = (string)('INSERT INTO LOG (ERROR) values ("Way down here")');
mysqli_query($conn, $sql);
$result = mysqli_query($conn, "SELECT LAST_INSERT_ID() LID");
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$outp["LID"] = $row["LID"];
}
} else {
echo "Sorry";
}
}
$sql = (string)('INSERT INTO LOG (ERROR) values ("'.$outp["LID"].'")');
mysqli_query($conn, $sql);
mysqli_close($conn);
echo json_encode($outp);
If anyone has an idea of what I'm doing wrong, I'd be extremely grateful. Please let me know if there are any other parts of the code that would be helpful to see. I've verified that the JSON being sent is valid already.
EDIT
Could $.post() potentially do anything for me that my current AJAX wouldn't? I don't see why it would, but then again I don't see the bug in my code either.
EDIT 2
I've updated my PHP code to what it is now (the edited portions are farthest to the left). I've also added the SUCCESS and ERROR callbacks to the AJAX. Basically I'm just using my LOG table to make sure that I'm where I should be. Every single insert in that PHP code inserts successfully, including the data that I'm echoing to be caught by the AJAX. Which I would normally take to mean that it is running to the end.
Side note, I'm using Visual Studio. When I was using IE for debugging, the alerts inside all of the AJAX callbacks would not fire. I switched to Chrome, and they were able to be caught and did fire.
In the Chrome > Network Tool, there were two entries. First, a call to processing.php, which was red. There was a second call to index.html?yearsGroup1=2016&yearsGroup2=2016..., followed by all of the variables on the page in the style of a GET request [which I now realize is just the page reloading itself after SUBMIT].
Finally, when I inspected err in the AJAX -> ERROR callback, I saw:
Object {readyState: 0, responseJSON: undefined, status: 0, statusText: "error"}
I'm not too sure where to go from here.
So for whatever reason, when I transferred my code over to a different folder, a non-breaking warning started to appear:
Warning: Unknown Property access is not allowed yet in Unknown on line 0
At which point I put my head through my monitor. PSA: Don't be an idiot like me, always check your PHP 15 extra times for errors.
I apologize to everyone who commented on this for wasting your time on my general incompetence. Thank you all again for your help.