Link Posted February 18, 2008 Share Posted February 18, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/91792-php-ajax/ Share on other sites More sharing options...
sKunKbad Posted February 19, 2008 Share Posted February 19, 2008 I always validate the form using regular javascript, then send the full post to the php script where it is validated a second time. It may seem like overkill, but you can't rely on javascript being on, and therefore can't rely on AJAX working. Quote Link to comment https://forums.phpfreaks.com/topic/91792-php-ajax/#findComment-470149 Share on other sites More sharing options...
resago Posted February 19, 2008 Share Posted February 19, 2008 you can post with ajax too. Quote Link to comment https://forums.phpfreaks.com/topic/91792-php-ajax/#findComment-470170 Share on other sites More sharing options...
Link Posted February 19, 2008 Author Share Posted February 19, 2008 But how do you do that? Like, how to I pass the $_POST to another PHP file? Quote Link to comment https://forums.phpfreaks.com/topic/91792-php-ajax/#findComment-470198 Share on other sites More sharing options...
phpSensei Posted February 19, 2008 Share Posted February 19, 2008 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); } Quote Link to comment https://forums.phpfreaks.com/topic/91792-php-ajax/#findComment-470201 Share on other sites More sharing options...
Link Posted February 19, 2008 Author Share Posted February 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/91792-php-ajax/#findComment-470204 Share on other sites More sharing options...
phpSensei Posted February 19, 2008 Share Posted February 19, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/91792-php-ajax/#findComment-470213 Share on other sites More sharing options...
Link Posted February 19, 2008 Author Share Posted February 19, 2008 Because it is a sliding form, and there are fields on each page. And the fields on each of the subsequent pages relies on the selections on the previous page, so there are tons of combinations. Quote Link to comment https://forums.phpfreaks.com/topic/91792-php-ajax/#findComment-470221 Share on other sites More sharing options...
sKunKbad Posted February 19, 2008 Share Posted February 19, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/91792-php-ajax/#findComment-470267 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.