PHP detect javascript, BUT no forms please

Associate
Joined
18 Oct 2002
Posts
1,312
Location
Milton Keynes
Hi all

I have a register form I want to detect if javascript is on or off with. Reason being I want it to either show a nice AJAX form, or something a bit more simple for non JS users.

The current JS detection I use is as follows:

PHP:
<?
if (isset($_POST['jstest'])) {
	$nojs = FALSE;
} else {
	// create a hidden form and submit it with javascript
	echo '<form name="jsform" id="jsform" method="post" style="display:none">';
	echo '<input name="jstest" type="text" value="true" />';
	echo '<script language="javascript">';
	echo 'document.jsform.submit();';
	echo '</script>';
	echo '</form>';
	// the variable below would be set only if the form wasn't submitted, hence JS is disabled
	$nojs = TRUE;
}



if ($nojs){
	//NO JS
	include('register_php.php');
}
else {
	//WITH JS
	include('register_ajax.php');
}
?>

This works fine BUT, if the form submit fails (if certain requirements arent met), it will send info back to the form as a POST, which is swiftly wiped clean when the JS detect form is done lol

Took me about an hour to realise why my AJAX form wasnt getting any errors, but wasnt submitting to the DB either. Now I am stuck, cant seem to find an alternative method.

Other people MUST have had the same problem, unless that is they just dont provide a backup to AJAX forms? Also if I redo my current error reporting to not use POSTS it will mess up numerous other forms in the site

Any ideas? :cool:
 
Why not just use Javascript to add the functionality to the plain form, like every other web application in the history of mankind?
 
sorry how do you mean? have been working on this all day so brain is fried and got a serious case of tunnel vision :p

do you mean have the plain form, and then get JS to add the fancy bits, so if JS is off it simply wont implement them?

if so because of the way I have done it, the layout / structure is different from one form to the other. wanted to keep a completely JS clean version, and have a nicer AJAX one seperately, that way its not all integrated and becomes a pain to go through 6 months later when its not fresh in my mind
 
that's not the way I would do it. make a functional non-js version then do all the lovely js over the top of it so that you know it's going to work on computers that can't handle JS for whatever reason.
 
Code:
<form onsubmit="javascript:someFancyXmlHttpRequestFunction(); return false">
nojs users will proceed with the normal submit. JS users won't.
 
Back
Top Bottom