Form submits after Javascript validation (too early)
Moderator: General Moderators
Form submits after Javascript validation (too early)
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?
What's the fix?
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
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.
Here is the code:
There, of course, are the other elements (submit, form name, etc) but I call them from different places in my php doc.
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>Here is the php form:
$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.
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='';
}
?>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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
you should take a look at http://ca3.php.net/register_globals
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
In general:
and in your function
It's the true's and false's that need to be in the right places.
Good luck!
Code: Select all
<form name="someForm" action="someFile.php" onSubmit="return someFunction()">
//Form contents
<input type="submit" name="submit" value="Go">Code: Select all
function someFunction() {
if (someEl != 'someValue') {
alert('You did it wrong dude!');
return false;
} else {
return true;
}
}Good luck!
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
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
Code: Select all
return false;viewforum.php?f=13
is the Client Side forum
Last edited by Chris Corbyn on Sun Dec 26, 2004 1:14 pm, edited 1 time in total.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact: