TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Javascript, AJAX, E4X (http://www.talkphp.com/javascript-ajax-e4x/)
-   -   Returning Correct Content (http://www.talkphp.com/javascript-ajax-e4x/1294-returning-correct-content.html)

CMellor 10-11-2007 10:49 PM

Returning Correct Content
 
Hey,

Am a bit stuck, though nothing major, hope I can get some assistance. Can I show you these four functions:

Code:

        function updateForm(element, msg) {
          $(element).request({
            onSuccess: function() {
                  $(element).disable()
                  alert(msg);
                  $(element).enable()
                }
          });
        }
       
        function updateSpryForm(form, msg, captcha) {
          if(Spry.Widget.Form.validate(form) == true) {
            if(captcha == true) {
                  verifyCaptcha(form, msg);
                  return false;
                }
                else {
                  updateForm(form, msg);
                }
          }
        }
       
        function verifyCaptcha(element, msg) {       
          $(element).request({
            onSuccess: function(transport) {
                  if(transport.responseText == 0) {
                    alert('Your security code does not match');
                  }
                  else {
                    updateForm(element, msg);
                        $(element).reset();
              }
                }
          });
        }
       
        function checkPass(element) {
          $(element).request({
            onSuccess: function(transport) {
                  if(!$F('currentPassword')) {
                    return false;
                  }
                  else if(transport.responseText == 2) {
                    alert('Your given password is incorrect');
                        $('currentPassword').activate();
                  }
                }
          });
        }

On my form tag, I have an onSubmit and I add two functions to it:

Code:

<form action="configs/ajax_functions.php?settings=password" method="post" id="passwordSecurity" onsubmit="checkPass(this); updateSpryForm(this, 'Your password was changed. You have been logged out, please log back in with your new password', false); return false">
What I want, is for when submit is pressed, it first uses the checkPass function, and if that returns the error alert, then nothing else happens. Currently, if the error alert appears, another alert appears confirming that all went well, and that's been generated from the updateForm function, which is used in updateSpryForm function.

Urm, hope I made sense, and didn't boggle you with too much code.

Look forward to a response.

Karl 10-12-2007 02:25 AM

Hi, try this function (add it with your others):

Code:

function validate()
        {
                try
                {
                        checkPass(this);
                        updateSpryForm(this, 'Your password was changed. You have been logged out, please log back in with your new password', false);
                }
                catch (szException)
                {
                        alert(szException);
                }
               
                // I'm assuming this is intentional?
                return false;
        }

Then replace the onsubmit event with the following code:

Code:

onsubmit="validate(); return false;"
Finally replace your checkPass() function with this revised one:

Code:

function checkPass(element) {
          $(element).request({
            onSuccess: function(transport) {
                  if(!$F('currentPassword')) {
                    return false;
                  }
                  else if(transport.responseText == 2) {
                        $('currentPassword').activate();
                    throw 'Your given password is incorrect';
                  }
                }
          });
        }

WHat this does is when the onsubmit event is raised it will call the validate function. It will then try to run the checkPass function followed by the updateSpryForm function. If the checkPass function fails we trhow an exception, rather than using an alert and returning false (your already using return false and I didn't know if that is for a specific reason). I hope this makes sense and most of all works, im a little tired, it's 03:30 :o

CMellor 10-13-2007 05:37 PM

Hey,

I tried your method:

Code:

function checkPass(element) {
          $(element).request({
            onSuccess: function(transport) {
                  if(!$F('currentPassword')) {
                    return false;
                  }
                  else if(transport.responseText == 2) {
                    throw 'Your given password is incorrect';
                        $('currentPassword').activate();
                  }
                }
          });
        }
       
        function validate() {
          try {
            checkPass(this);
                updateSpryForm(this, 'Your password was changed. You have been logged out, please log back in with your new password', false);
          }
          catch(exception) {
            alert(exception);
          }
        }

When I click the submit button, it brings up an error and says TypeError: $(element).request is not a function

Karl 10-14-2007 03:57 PM

Hi there, sorry, made a stupid mistake, ammend the function like this:

Code:

function validate(pForm)
        {
                try
                {
                        checkPass(pForm);
                        updateSpryForm(pForm, 'Your password was changed. You have been logged out, please log back in with your new password', false);
                }
                catch (szException)
                {
                        alert(szException);
                }
               
                // I'm assuming this is intentional?
                return false;
        }

and then ammend the onsubmit so it looks like:

Code:

onsubmit="validate(this); return false;"
Lets see if that works :)

CMellor 10-14-2007 06:48 PM

Hey,

That did indeed sort out that problem... I thought it might be that, but didn't have time to check it out last night.

Any who, what's happening now is, it is indeed calling the AJAX function, but it doesn't display the alert, at all, so it then just moves onto the updateSpryForm function. I'm new to the Try... Catch, Throw method, so I'm sure it's something to do with that.

Karl 10-15-2007 10:43 AM

I dont think this is the cause, but you changed the order of execution near the throw statemenet. Here's what I originally posted:

Code:

else if(transport.responseText == 2) {
                        $('currentPassword').activate();
                    throw 'Your given password is incorrect';
                  }

Notice how we throw the exception after we call the .activate() method. We do this because as soon as we throw the exception we leave the method immediately after (no code after the throw statement is read).

As for the current issue, im not actually sure what is causing that. The try..catch should be working fine. If you'd like to test it, try ammending the checkPass function like so:

Code:

function checkPass(element) {
throw 'Test Alert';

That should throw a new exception as soon as we enter the checkPass function. If the alert shows, it is most likely a logic problem, i.e. neither the "if" or "else if" conditions are true.

CMellor 10-15-2007 10:29 PM

Hey,

Just wanted to let you know I appreciate the help you've given me on this little thing, but it's been real stubborn and doesn't want to work, so I'm gonna leave it, I've spent way too long on it, and it's just doing my head in.

I tried adding a throw where you said, and when I clicked submit, that alert appeared. I added an alert after the if(transport.responseText == 2) and when I submit, it the responseText is 2, it will show the error, but when I click that, it moves onto the next function... but that was the case before the Try... Catch method.

Thanks again, but yeah... I'll forget it for now.


All times are GMT. The time now is 04:04 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0