www.webdeveloper.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2010
    Posts
    14

    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

    PHP Code:
    <?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:
    // 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;
       
    });
    });

  2. #2
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    10,413
    Hi,

    As far as jQuery is concerned, the Ajax request is always successful if the HTTP result is code 200. 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 HTTP status code: 404 (file not found), then your failure function would trigger instead.

    To solve your problem, write some code in your
    success 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.)
    Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles