Jump to content

[SOLVED] Stop form from processing?


Adeus

Recommended Posts

I have a step-based process set up. Each page has a form which includes a hidden field that sends it to the next step upon form submission. It works fine, but I ran into a problem when I implemented JavaScript validation.

 

Here is the JavaScript that validates a number no greater than 4 is entered:

 

<script type="text/javascript">
function validate_number(field,alerttxt) {
with (field) {
	if (value > 4)
			{alert(alerttxt);return false;}
	else {return true}
}
}

function validate_form(thisform) {
with (thisform) {
	if (validate_number(sol_10,"Sorry, the limit is 4.")==false)
  			{quantity.focus();return false;}
	}
}
</script>

 

And within the body:

<?php //STEP 10
if ($step==110) { 
$sol10 = (isset($_SESSION['sol_10'])) ? $_SESSION['sol_10'] : "";
?>
<div id="question">
<p>How many?</p>
<form action="test.php" method="get" onSubmit="return validate_form(this)" >
<p>Sol_10: <input type="text" name="sol_10" value="<?php echo $sol10; ?>" /></p>
<input type="hidden" name="step" value="111" />
<input type="submit" value="Next Step" />
</form>
</div>
<?php } ?>

 

When the form is submitted with a number greater than 4, I get the alert, but it still proceeds to the next step. Is there a way to stay on the current step (or ignore the hidden field if quantity's value > 4)?

Link to comment
Share on other sites

the problem with onsubmit is that if there is an error in your JS, it will still send the form and you won't see the JS error cus the page changes.

 

for debugging stuff like this, either use FireBug to execute the validation or create an extra button to test:

validate_form(document.forms[0]);

you will see there is an error in this line:

           {quantity.focus();return false;}

there is no 'quantity' field in your form. should this be 'sol_10' instead?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.