Advertisement

04.22.2008 at 01:57AM PDT, ID: 23342185
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.2

Help with prototype Ajax.Request function

Asked by aoline in JavaScript, Scripting Languages, Web Development

Tags:

I am writing an AJAX form script for a website but I need some help with the main Ajax.Request function.

The application uses a series of web forms ie page1, page2, page3 etc.

Each web form is standard HTML and probably contains just 2-3 fields on each page. These fields can be text, option, textarea etc.

The html form fields may change so the script needs to stay generic.

The html web form is clean and has no inline javascript except to include the prototype.js and the custom Ajax.Request js etc. I've currently set up my custom js to loop through and set up the event listeners and this is all fine.

What I need the form to do is as follows:

1) I want each form field to be validated onblur by sending the request through Ajax.Request to the server side php script. The PHP is fine - I don't need help with that.

2) When all fields are validated and the user presses the submit button the whole form should be submitted via the Ajax.Request and I'll handle it on the PHP side.

3) If the user does not have javascript enabled in their browser the html form should still be able to submit all the fields normally and my php will handle the data.

Your job is to write the Ajax.Request related function(s) that allows these requests to submit only the correct data as GET or POST through to the php and handle the response from the PHP catering for the 3 scenarios above.

Thanks Matthew

The current js is as follows:

Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
Part 1 of the script not posted here simply contains the vars.
Part 2 of the script not posted here simply loops through the html form and sets up the event listeners eg:
 
$(elementList[n].id).observe('blur', respondToBlur); //triggers the validation request.
 
respondToBlur calls:
goValidate(element); //do the ajax request
 
Part 3 of the script is my ajax request:
 
	function goValidate(element) { 
	
		// 1. prepare the ajax Request
		var actionURL = action; // the php validation script
		//var actionURL = action + '?id=' + element.id + '&value=' + $F(element);
	
		// 2. now do the ajax Request
		new Ajax.Request(
			actionURL, //url
			{
			
			//method:method,
			parameters:Form.serialize(formID), // this takes all the POST fields and sends them to the controller.php
			asynchronous:true,
			requestHeaders: ['Cache-Control', 'no-cache', 'Pragma', 'no-cache'],
 
			onComplete: function(request) {
				// check the incoming literal string format eg pass|Good message or fail|Sorry bad message and update accordingly
				var fullMessage = request.responseText;
				var myArray = fullMessage.split('|'); //split items at comma to array
				var successCode = myArray[0]; // fail or pass
				var messageText = myArray[1]; // eg good message
				if (successCode == 'pass') {
					// 1. successful validation, now UPDATE THE MESSAGE FIELD
					if (messageText) { 
						$(updateElem).innerHTML = messageText; // add message to message div
					}
 
				} else if (successCode == 'fail') { // PHP validation failed
					// 1. validation failed so update the message field
 
					if (messageText) {
						$(updateElem).innerHTML = messageText; // add message to message div
					}
 
				} else {
					//system failure!
					alert("Ooops, something went wrong with the PHP response as no error code was found - probably a config or server error so better log something! " + fullMessage);
				}
			
			}	
		});	
	
		//only do this validating part when the first stage of the ajax request has been sent.
	}//ends function goValidate
[+][-]04.27.2008 at 01:27AM PDT, ID: 21448505

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.27.2008 at 01:29AM PDT, ID: 21448508

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.27.2008 at 01:34AM PDT, ID: 21448517

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.27.2008 at 11:37PM PDT, ID: 21451912

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 11:45AM PDT, ID: 21456519

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 12:37PM PDT, ID: 21456958

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 01:23PM PDT, ID: 21457305

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: JavaScript, Scripting Languages, Web Development
Tags: JavaScript
Sign Up Now!
Solution Provided By: wilq32
Participating Experts: 1
Solution Grade: B
 
 
[+][-]07.11.2008 at 08:35PM PDT, ID: 21987534

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628