I am having trouble getting my jQuery ajax to work properly. It directs to the PHP page to update the database, but never returns back to the script for the success or error options.
My code is below:
$(document).ready(function(){ $("form#updatejob").submit(function() { function textreplace(x) {return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");} // we want to store the values from the form input box, then send via ajax below var job = $("#job").attr("value"); var description = $("#description").val(); description.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); var startDate = $("#startDate").attr("value"); var releaseDate = $("#releaseDate").attr("value"); var status = $("#status").attr("value"); $.ajax({ beforeSend:textreplace(description), type: "POST", url: "updatedjob.php", data: "jobID="+ job +"& description="+ description +"& startDate="+ startDate +"& releaseDate="+ releaseDate +"& status="+ status, success: function(){ $("form#updatejob").hide(function(){$("div.success").fadeIn();}); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert("Status: " + textStatus); alert("Error: " + errorThrown); } }); return false; }); });
And the PHP:
<?php include("connect.php"); $job = trim($_POST['job']); $startDate = trim($_POST['startDate']); $releaseDate = trim($_POST['releaseDate']); $mysqlstartdate = date('Y-m-d', strtotime($startDate)); $mysqlreleasedate = date('Y-m-d', strtotime($releaseDate)); $description = trim($_POST['description']); $status = trim($_POST['status']); $update = "UPDATE jobs SET startDate = '$mysqlstartdate', releaseDate = '$mysqlreleasedate', description = '$description', status = '$status' WHERE jobID = '$job' "; $rsUpdate = mysql_query($update); // or die(mysql_error()); mysql_close(); ?>
ajax post method. The reason was my response was not in the JSON format so there was no need for the dataType: 'json' line in the submit method. In my case, the returned response was in text format that's why it was not going to success event. Solution: Remove dataType: 'json' line.
The best way to bubble that error from the server side (using php) to the client side is to send a header through the Ajax request somewhere in the 400's (which is always associated with errors). Once the Ajax request receives this it will trigger your error function.
ajax method lets you set a timeout in milli seconds. When a timeout happens, The fail callback is called, with errorThrown set to "timeout". The request is aborted, meaning that even if the response arrives later on, your done callback is not called by jQuery.
Try this:
$.ajax({ beforeSend: function() { textreplace(description); }, type: "POST", url: "updatedjob.php", data: "jobID="+ job +"& description="+ description +"& startDate="+ startDate +"& releaseDate="+ releaseDate +"& status="+ status, success: function(){ $("form#updatejob").hide(function(){$("div.success").fadeIn();}); }, error: function(XMLHttpRequest, textStatus, errorThrown) { alert("Status: " + textStatus); alert("Error: " + errorThrown); } });
The beforeSend
property is set to function() { textreplace(description); }
instead of textreplace(description)
. The beforeSend
property needs a function.
One also may use the following to catch the errors:
$.ajax({ url: url, success: function (data) { // Handle success here $('#editor-content-container').html(data); $('#editor-container').modal('show'); }, cache: false }).fail(function (jqXHR, textStatus, error) { // Handle error here $('#editor-content-container').html(jqXHR.responseText); $('#editor-container').modal('show'); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With