/    Sign up×
Community /Pin to ProfileBookmark

jquery callback success/error not working

For some reason the success and error callbacks are not working at all. I’m using jquery.ajax to submit a form w/o page refresh through a php file. I have set the success & error callbacks but even when I changed the mysql password (to a incorrect one) I got the success message :S

here’s the php & js script

[code=php]<?php

/*$fp = fopen(‘data.txt’, ‘a+’);
fwrite($fp, ‘ok’);
fclose($fp);*/

$sections = $_REQUEST[‘sections’];
$fontColor = $_REQUEST[‘fontColor’];
$bgcolor = $_REQUEST[‘bgcolor’];
$font = $_REQUEST[‘font’];
$fontSize = $_REQUEST[‘fontSize’];
$lineHeight = $_REQUEST[‘lineHeight’];
$letterSpacing = $_REQUEST[‘letterSpacing’];
$fontStyle = $_REQUEST[‘fontStyle’];
//$tekst = $_REQUEST[‘tekst’]

$connect = mysql_connect(“localhost”, “root”, “password”) or die (“Unable to connect to the database”);
mysql_select_db(“ipsum”) or die (“doesnt exist: ” . mysql_error());

$update = “INSERT INTO con (Sections, Fontcolor, BackgroundColor, Font, FontSize, LineHeight, LetterSpacing, FontStyle) VALUES (‘”.$sections.”‘, ‘”.$fontColor.”‘, ‘”.$bgcolor.”‘, ‘”.$font.”‘, ‘”.$fontSize.”‘, ‘”.$lineHeight.”‘, ‘”.$letterSpacing.”‘, ‘”.$fontStyle.”‘)”;

// the content of ‘data.txt’ is now 123 and not 23!

mysql_query($update) or die (“unable to update: ” . mysql_error());

mysql_close($connect);

?>[/code]

[code]// JavaScript Document

$(function(){
$(‘.save’).click(function(){

var sections = $(‘#sections’).attr(‘value’);
var fontColor = $(‘#fontColor’).attr(‘value’);
var bgcolor = $(‘#bgcolor’).attr(‘value’);
var font = $(‘#font’).attr(‘value’);
var fontSize = $(‘#fontSize’).attr(‘value’);
var lineHeight = $(‘#lineHeight’).attr(‘value’);
var letterSpacing = $(‘#letterSpacing’).attr(‘value’);
var fontStyle = $(‘#fontStyle’).attr(‘value’);
//Datastring to pass through

var dataString = {sections:sections, fontColor:fontColor, bgcolor:bgcolor, font:font, fontSize:fontSize, lineHeight:lineHeight, letterSpacing:letterSpacing, fontStyle:fontStyle};

$.ajax({
type: “POST”,
url: “update.php”,
data: dataString,
success: (function(){$(‘#successMessage’).fadeIn(“slow”).delay(1000).fadeOut(2000);}),
error: (function (){$(‘#errorMessage’).fadeIn(“slow”).delay(1000).fadeOut(2000);}),

});
return false;

});
});[/code]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@JonaOct 06.2010 — [font=arial]Hi,

As far as jQuery is concerned, the Ajax request is [b]always successful if the HTTP result is code 200[/b]. In other words, the failure function will only trigger on an HTTP error page. If, for example, you changed the URL to "file_that_doesnt_exist.php," a file that doesn't exist and returns an [b]HTTP status code: 404 (file not found)[/b], then your failure function would trigger instead.

To solve your problem, write some code in your [/font][font=monaco]success[/font][font=arial] function that distinguishes when your result is favorable and when it is not. (In other words, check if the data returned from "update.php" contains the phrase "unable to update." If it does, then you should proceed with an action that indicates the server-side functionality failed to meet expectations; otherwise, proceed with the default action for your success function.)[/font]
×

Success!

Help @cinek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...