After 7 hours of experimenting and reading, I'm stumped:
Here's the really short version:
If onSubmit doesn't execute because JavaScript is disabled, does its presence in the form tag somehow prevent the values in hidden input fields from being passed along via POST?
If you can answer from the above summary, you might not need the following, but here's why I ask:
I have a form where the user must answer all 12 multiple-choice questions by selecting radio buttons, or the results will not be accurate.
In the first version, I had PHP check that all questions were answered and send an error message if any were not. The only drawback was that, if the user did omit an answer, he/she would have to select all of them again. I could get around this by storing them in MySQL and building the questions dynamically, but I'd like to see if JavaScript can help out.
When I add a JavaScript validation via onSubmit, it warns the user of an omission before the page is submitted, which does the trick.
Here's the problem: If the user has JavaScript disabled, when he/she hits the SUBMIT button, not only does the JavaScript validation not occur (of course), but the backup PHP validation also gets skipped. The results page shows up without any indication of questions not answered. Those few users will get inaccurate results, which is a lot worse than having to re-select all of the buttons. (I did the JavaScript disabled test in Netscape 4.7 in Windows ME.)
I use a PHP $seeform variable to govern whether the input form or the results are displayed.
Here's the basic structure:
........................................
if ( $seeform != "no" ) {
// show questions
}
// submit questions and, if all answered, analyze
echo "<FORM name=\"userInfo\" method=\"POST\" action=\"$PHP_SELF\" onSubmit=\"return validateProfile()\">";
//when form submitted, $seeform is set to no
<INPUT type="hidden" name="seeform" value="no">
// JavaScript checks to see if all questions answered
function validateProfile() . . .
// if any question lacks a checked selection, stop submit
{
alert("Please make sure to answer every question."
return false;
}
// if every question has an answer, submit allowed
return true;
}
// PHP checks to see if all questions answered
// if any missed, questions displayed with error message
if ( !$complete ) {
echo "<b>Please make sure to answer [missed] questions.</b>";
$seeform = "yes";
}
else
// show results
...............................
One solution I thought of was to get JavaScript to write the onSubmit part of the form tag so that, if a user was not running JavaScript, that part of the tag would not exist and thus would not cause the PHP validation to be skipped, but after many attempts, I couldn't figure out exactly how to write that JavaScript within the form tag.
Questions:
1. How might I prevent the onSubmit event in the form tag from causing the PHP validation to be skipped when the user has JavaScript disabled? This behavior surprised me. Is there some particular structure that causes this, and that I might avoid?
2. If the solution is to write the onSubmit event into the form tag using JavaScript, how might I write it?
Any suggestions would be greatly appreciated.
Here's the really short version:
If onSubmit doesn't execute because JavaScript is disabled, does its presence in the form tag somehow prevent the values in hidden input fields from being passed along via POST?
If you can answer from the above summary, you might not need the following, but here's why I ask:
I have a form where the user must answer all 12 multiple-choice questions by selecting radio buttons, or the results will not be accurate.
In the first version, I had PHP check that all questions were answered and send an error message if any were not. The only drawback was that, if the user did omit an answer, he/she would have to select all of them again. I could get around this by storing them in MySQL and building the questions dynamically, but I'd like to see if JavaScript can help out.
When I add a JavaScript validation via onSubmit, it warns the user of an omission before the page is submitted, which does the trick.
Here's the problem: If the user has JavaScript disabled, when he/she hits the SUBMIT button, not only does the JavaScript validation not occur (of course), but the backup PHP validation also gets skipped. The results page shows up without any indication of questions not answered. Those few users will get inaccurate results, which is a lot worse than having to re-select all of the buttons. (I did the JavaScript disabled test in Netscape 4.7 in Windows ME.)
I use a PHP $seeform variable to govern whether the input form or the results are displayed.
Here's the basic structure:
........................................
if ( $seeform != "no" ) {
// show questions
}
// submit questions and, if all answered, analyze
echo "<FORM name=\"userInfo\" method=\"POST\" action=\"$PHP_SELF\" onSubmit=\"return validateProfile()\">";
//when form submitted, $seeform is set to no
<INPUT type="hidden" name="seeform" value="no">
// JavaScript checks to see if all questions answered
function validateProfile() . . .
// if any question lacks a checked selection, stop submit
{
alert("Please make sure to answer every question."
return false;
}
// if every question has an answer, submit allowed
return true;
}
// PHP checks to see if all questions answered
// if any missed, questions displayed with error message
if ( !$complete ) {
echo "<b>Please make sure to answer [missed] questions.</b>";
$seeform = "yes";
}
else
// show results
...............................
One solution I thought of was to get JavaScript to write the onSubmit part of the form tag so that, if a user was not running JavaScript, that part of the tag would not exist and thus would not cause the PHP validation to be skipped, but after many attempts, I couldn't figure out exactly how to write that JavaScript within the form tag.
Questions:
1. How might I prevent the onSubmit event in the form tag from causing the PHP validation to be skipped when the user has JavaScript disabled? This behavior surprised me. Is there some particular structure that causes this, and that I might avoid?
2. If the solution is to write the onSubmit event into the form tag using JavaScript, how might I write it?
Any suggestions would be greatly appreciated.