Adeus Posted March 10, 2009 Share Posted March 10, 2009 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)? Quote Link to comment https://forums.phpfreaks.com/topic/148811-solved-stop-form-from-processing/ Share on other sites More sharing options...
rhodesa Posted March 10, 2009 Share Posted March 10, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/148811-solved-stop-form-from-processing/#findComment-781438 Share on other sites More sharing options...
Adeus Posted March 10, 2009 Author Share Posted March 10, 2009 D'oh! Time to stop copying old scripts and start paying attention to detail. Thank you, much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/148811-solved-stop-form-from-processing/#findComment-781466 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.