SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question AJAX Function: returning boolean values

    Hey guys,

    I'm trying to write a AJAX Function for validating a form, but I want it to work in a way which is different from most approaches.
    Well, most people replace the action attribute in the form tag with javascript:ajax_function();. The function will now display an alert() if the server side script returns an error or updates the page if the form was filled in correctly.
    Well, that's how I did it before and it works .

    What I want to do now though, is a bit different.
    I want to leave action attribute as it is, as a normal http address. To the submit button I added onClick="return ajax_function();".
    I still want to display the alert() and return false if something goes wrong, but otherwise I now need the function to return TRUE instead of redirecting the page. I thus simply replaced the content of the handler function with return true; instead of the page-update-code.
    ..but is doesn't work

    I suppose it's because of the structure of AJAX functions. Because of the
    http.onreadystatechange = handlerFunction;
    the ajax function itsself can't actually do a RETURN, can it?? It always calls a handler function which makes a RETURN impossible. To get my code working I need the ajax function ittself preform the RETURN though.

    How can I achieve this?

    Thanks for your help!

    Cheers, Florian

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, most people replace the action attribute in the form tag with javascript:ajax_function();
    I've never seen any posts to that effect. Most people do validation onsubmit, which seems to make some sense.

    but otherwise I now need the function to return TRUE instead of redirecting the page.
    Huh?

    To get my code working I need the ajax function ittself preform the RETURN though.
    Call a function onsubmit. In that function, make an XMLHttpRequest. Depending on what happens, return true or false from the function. False will cancel the submit. True will allow the submit to continue and the page specified in the action attribute will load in the browser. If you don't want the page specified to load in the browser under any circumstances, then always return false from the function.

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Copied from http://www.w3schools.com/dom/dom_http.asp


    Most of the examples here use the async mode (the third parameter of open() set to true).

    The async parameter specifies whether the request should be handled asynchronously or not. True means that script continues to run after the send() method, without waiting for a response from the server. false means that the script waits for a response before continuing script processing. By setting this parameter to false, you run the risk of having your script hang if there is a network or server problem, or if the request is long (the UI locks while the request is being made) a user may even see the "Not Responding" message. It is safer to send asynchronously and design your code around the onreadystatechange event!

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanx for your replies.

    Alrighty, I'm now calling the function validate() via onSubmit but that doesn't solve the problem of course.

    The async parameter doesn't change anything either.

    The function validate() itsself calls an ajax-class.

    I described the problem in an diffrent thread:
    http://www.sitepoint.com/forums/showthread.php?t=358687

    Cheers, Flo

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I described the problem in an diffrent thread:
    In IE6, a function called 'name' will cause an error when you try to call it from an onclick event handler, not to mention the other error in your code. If you fix those errors, the function alerts true.

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    well, it's a "model" code...a simple abbreviation of the functions/classes i'm using
    To put all the code here whould definitely take up too much space.

    Cheers, Florian


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
  •