-2

So my ajax success function isn't loading. So I have no idea what's wrong with it. Is there any way I can check that? Like with mysql_error()? For now, if you guys can figure out what is wrong with it, or the PHP that's on the other end, that would be great.

jQuery.ajax({
   type: "POST",
   url: "http://mywebsite.net/snippetsave.php",
   data: {id: snippetID, name: snippetName, content: snippetContent},
   cache: false,
   success: function(response){
        loadsnippet(snippetID);
        $("body").attr("saved", "yes");
        $("a[snippetid='" + snippetID + "']").addClass("saved");
   }
 });

and snippetsave.php:

<?php
session_start();
$username = $_SESSION['username'];
$password = $_SESSION['password'];
$connect = mysql_connect(......);
$select_db = mysql_select_db(.....);
if(!$connect){ 
die(mysql_error()); 
} 
$currentsnippet = mysql_escape_string($_POST['id']);
$snippetnames = mysql_escape_string($_POST['name']);
$snippetcontents = mysql_escape_string($_POST['content']);
$update = mysql_query("UPDATE newsnippets SET name='$snippetnames', content='$snippetcontents' WHERE id='$currentsnippet'");
if (!$update){
die(mysql_error());
}
?>
imp
  • 460
  • 2
  • 7
  • 23
  • 2
    **:==>** http://stackoverflow.com/questions/1844370/jquery-handle-fallback-for-failed-ajax-request/1844401#1844401 `:)` & http://api.jquery.com/ajaxError/ + I am not the downvoter! have fun! #also_read_about_Single origin policy and CORS stuff! – Tats_innit Oct 20 '13 at 00:53
  • 1
    already answered, but thanks :) – imp Oct 20 '13 at 00:58
  • Btw, is it normal for people to downvote what might seem like stupid questions? Like, I thought this website was not-so-experienced people as well. Isn't this a perfectly acceptable question? – imp Oct 20 '13 at 01:01

2 Answers2

1

Try adding error handler function:

jQuery.ajax({
   type: "POST",
   url: "http://mywebsite.net/snippetsave.php",
   data: {id: snippetID, name: snippetName, content: snippetContent},
   cache: false,
   success: function(response){
        loadsnippet(snippetID);
        $("body").attr("saved", "yes");
        $("a[snippetid='" + snippetID + "']").addClass("saved");
   },

   error: function(response) {console.log(response);}
 });
Tomasz Kapłoński
  • 1,320
  • 4
  • 24
  • 49
0

you can add a callback method in jQuery.ajax method

jQuery.ajax({
    type: "POST",
    url: "http://mywebsite.net/snippetsave.php",
    data: {id: snippetID, name: snippetName, content: snippetContent},
    cache: false,
    success: function(response){
             loadsnippet(snippetID);
             $("body").attr("saved", "yes");
             $("a[snippetid='" + snippetID + "']").addClass("saved");
    },
    error:function(){
            // error handler
    }
});
Hawk
  • 90
  • 1
  • 11