Jump to content

PHP & AJAX


Link

Recommended Posts

I want to basically check errors in a form using AJAX and PHP.  My question is, how do I get that info to the PHP function?  I know a common method is us JS on url: errors.php?name1=val1&name2=val2&...etc.  Is there not a more efficient way?  I am passing around 100 fields...

Link to comment
Share on other sites

But how do you do that?  Like, how to I pass the $_POST to another PHP file?

 

phpscript would be a var called

 

var phpscript = "login.php";

 

function sendRequestPost() {

	var user = document.getElementById('username').value;
	var pass = document.getElementById('password').value;

	// Open PHP script for requests
	http.open('post', phpscript);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponsePost;
	http.send('username='+ user +'&password='+ pass);

}

Link to comment
Share on other sites

So, basically, the simplest way is to pass each field to http.send()?  I can do that except for it's something like 112 different fields.

 

Why in the world would you need 112 fields, it just doesnt cross my mind...

 

Even validating would take you longer then the ajax itself, no matter what, it will be a long process.

 

Unless you do a foreach $_POST as $..etc

Link to comment
Share on other sites

Of course you can post with AJAX, but what if the user has javascript disabled?

 

This is what I have done in the past:

1) try to set a cookie with javascript before the user gets to the form.

2) on the form page, verify if the user has javascript enabled by checking for the cookie using php.

3) if javascript is disabled, post to a validation/cURL script and output a thank you or error message.

4) if javascript is enabled, post with AJAX to the validation/cURL script, output a thank you or error message.

 

This works, and it falls back on php in case javascript is disabled. The advantage is that the user doesn't have to wait for the lag of the validation/cURL script if they have javascript enabled.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.