Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax Success and Error function failure

Tags:

jquery

ajax

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();  ?> 
like image 589
michael Avatar asked Jan 18 '12 22:01

michael


People also ask

Why is Ajax success not working?

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.

How do you handle Ajax failure?

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.

What is Ajax failure?

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.


2 Answers

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.

like image 177
Matt Bradley Avatar answered Oct 18 '22 14:10

Matt Bradley


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'); }); 
like image 41
Max Avatar answered Oct 18 '22 13:10

Max