Page 1 of 2
Form submits after Javascript validation (too early)
Posted: Sat Dec 25, 2004 11:16 pm
by Hibba
I have a form that has several questions, all on seperate pages. After each submission (Next question) I have a javascript that checks to see if the data entered into the field was numeric. The javascript works, but once you hit enter (or click) the OK box in the javascript alert, it still proceeds to the next page in the form.
What's the fix?
Posted: Sun Dec 26, 2004 1:34 am
by feyd
show some code.
the basics however are (from memory, it's been awhile...):
use an onsubmit in the form tag as a form processor. Return false to indicate to not submit the form.
The better way to handle it is through the script being submitted to. Although it can require more communication between the user and the server, but avoids jerks bypassing your script and submitting garbage. Basically, if there's an error in their submission (non-numeric), you have the script kick out an error with the question again, maybe their answer too. When the answer is in the correct format, display the next question.
Posted: Sun Dec 26, 2004 9:19 am
by protokol
You need to show the JavaScript alert and then right afterwards return false. This will stop the form from submitting. If nothing is returned or you return true, then the form will submit like usual.
Posted: Sun Dec 26, 2004 12:03 pm
by Hibba
Here is the code:
Code: Select all
<head><SCRIPT LANGUAGE="JAVASCRIPT"><!--
function check(contents) {
if (((contents / contents) != 1) && (contents != 0))
{alert('Please enter only a number into this box \n (No commas or decimals)')}
}
//--></SCRIPT></head>
<p>How many years do you plan to be in your home?</p>
<p><input type="text" name="how_many" size="10" onBlur="check(this.value)"></p>
There, of course, are the other elements (submit, form name, etc) but I call them from different places in my php doc.
Posted: Sun Dec 26, 2004 12:09 pm
by Hibba
Here is the php form:
Code: Select all
<?php
if ($pass != 1) {
if (!empty($how_many)) $currentquestion = $question2;
if (!empty($purchase_price) AND !empty($down_payment)) $currentquestion = $question3;
if (!empty($credit_history)) $currentquestion = $question4;
if (!empty($budget)) $currentquestion = $question5;
if (!empty($month_debt) AND !empty($gross_income)) $currentquestion = $question6;
$form = "<form name ="myform" method="post" action="".$PHP_SELF."">"; //beginning of the form
$form .= $currentquestion;
if (!empty($how_many)) $form .= "<input type="hidden" name="how_many" value="".$how_many."">";
if (!empty($purchase_price) AND !empty($down_payment)) {
$form .= "<input type="hidden" name="purchase_price" value="".$purchase_price."">";
$form .= "<input type="hidden" name="down_payment" value="".$down_payment."">";
}
if (!empty($credit_history)) $form .= "<input type="hidden" name="credit_history" value="".$credit_history."">";
if (!empty($budget)) $form .= "<input type="hidden" name="budget" value="".$budget."">";
if (!empty($gross_income) AND !empty($month_debt)) {
$form .= "<input type="hidden" name="gross_income" value="".$gross_income."">";
$form .= "<input type="hidden" name="month_debt" value="".$month_debt."">";
$form .= "<input type="hidden" name="pass" value="1">";
}
$form .= "<input type="hidden" name="passer">";
$form .= "<input type="submit" value="Next" name="submit">";
$form .= "</form>"; //end of form
echo $form;
} else {
$error_msg='';
}
?>
$pass is found as a hidden variable in the last question
The other variables are found in the questions as well (included html) But basically, it goes through to see if something is submitted, if so what, then which question are you on now, display that question. If pass is true, you are on the last question, so check for some errors on that and your done.
Posted: Sun Dec 26, 2004 12:45 pm
by John Cartwright
A note on that script, it requires register globals to be ON. By default it is OFF.
Posted: Sun Dec 26, 2004 12:47 pm
by Hibba
Ok, I got this code from another forum. What needs the globals on?
Posted: Sun Dec 26, 2004 12:52 pm
by John Cartwright
Posted: Sun Dec 26, 2004 12:59 pm
by Hibba
ah, ok, you mean my PHP script. right, I didn't show all of the code, but those variables are fine. I have an extract($_POST) at the beginning...
So lets gets back to the problem at hand! Dern javascript!
Posted: Sun Dec 26, 2004 1:03 pm
by Chris Corbyn
In general:
Code: Select all
<form name="someForm" action="someFile.php" onSubmit="return someFunction()">
//Form contents
<input type="submit" name="submit" value="Go">
and in your function
Code: Select all
function someFunction() {
if (someEl != 'someValue') {
alert('You did it wrong dude!');
return false;
} else {
return true;
}
}
It's the true's and false's that need to be in the right places.
Good luck!

Posted: Sun Dec 26, 2004 1:06 pm
by Chris Corbyn
There's a separate forum for this stuff. Makes it easier for ppl to see what you want to know. I have a post sitting in there at the moment

Posted: Sun Dec 26, 2004 1:08 pm
by Hibba
And that would be what?
Posted: Sun Dec 26, 2004 1:11 pm
by Chris Corbyn
Because you're getting the alert your checks are obviously ok but you need the
in order to prevent form submission
viewforum.php?f=13
is the Client Side forum
Posted: Sun Dec 26, 2004 1:13 pm
by Hibba
Right, I added that, but still the same thing, it submits the form. Where is this other forum you mentioned?
Posted: Sun Dec 26, 2004 1:19 pm
by John Cartwright
the client side forum as posted 2 posts ago..