TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-11-2007, 10:49 PM   #1 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default 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.
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 10-12-2007, 02:25 AM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

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
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 10-13-2007, 05:37 PM   #3 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

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
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 10-14-2007, 03:57 PM   #4 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

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 :)
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 10-14-2007, 06:48 PM   #5 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

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.
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 10-15-2007, 10:43 AM   #6 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

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.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 10-15-2007, 10:29 PM   #7 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

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.
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:01 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design