Form submits after Javascript validation (too early)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Form submits after Javascript validation (too early)

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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.
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post by Hibba »

Here is the code:

Code: Select all

<head><SCRIPT LANGUAGE="JAVASCRIPT"><!--
function check(contents) &#123;
    if (((contents / contents) != 1) && (contents != 0)) 
    &#123;alert('Please enter only a number into this box \n (No commas or decimals)')&#125;
&#125;
//--></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.
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

A note on that script, it requires register globals to be ON. By default it is OFF.
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post by Hibba »

Ok, I got this code from another forum. What needs the globals on?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you should take a look at http://ca3.php.net/register_globals
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post 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!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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() &#123;
    if (someEl != 'someValue') &#123;
        alert('You did it wrong dude!');
        return false;
    &#125; else &#123;
        return true;
    &#125;
&#125;
It's the true's and false's that need to be in the right places.

Good luck! :-D
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;-)
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post by Hibba »

And that would be what?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Because you're getting the alert your checks are obviously ok but you need the

Code: Select all

return false;
in order to prevent form submission

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.
Hibba
Forum Commoner
Posts: 58
Joined: Fri Oct 29, 2004 11:37 pm

Post by Hibba »

Right, I added that, but still the same thing, it submits the form. Where is this other forum you mentioned?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

the client side forum as posted 2 posts ago..
Post Reply