dadamssg Posted January 23, 2009 Share Posted January 23, 2009 Ive created a form and want to require three fields be filled....and if not an alert comes up. The problem is the alert does come up but as soon as i click ok it still continues to the form action. how do i prevent that? heres the code <?php /*File: testdisplay.inc *Display test form */ include("sdateselectfun.php"); include("edateselectfun.php"); include("timeselectfuncs.php"); ?> <html> <head> <script> function ValidateFields() { var title = document.getElementById("title"); var description = document.getElementById("description"); var location = document.getElementById("location"); if (title.value == "") { window.alert("Please enter a Title."); title.focus(); return false; } if (description.value == "") { window.alert("Please enter a Description."); description.focus(); return false; } if (location.value == "") { window.alert("Please enter a Location."); location.focus(); return false; } return true; } </script> <title>Event Post Form</title> </head> <body> <br><br><center><h2><b>Event Post Form</b></h2></center> <center> <form action="checkpost1.php" method="POST" onsubmit="ValidateFields();"> <Table border='7'> <tr> <td><center><b>Title: </b></center></td> <td><input type="text" id="title" size="75" maxlength="77"></td> </tr> <tr> <td valign="top"><br><b> Description: </b></td> <td><textarea id='description' cols='57' rows='5'> </textarea></td> </tr> <tr> <td><center><b>Location: </b></center></td> <td><input type="text" id="location" size="75" maxlength="77"></td> </tr> <tr> <td colspan=2> </td> </tr> <tr> <td><b><center>Starts: </center></b></td><td align=center><?php sdateselect();?>    <?php stimeselect();?>        </td> </tr> <tr> <td colspan=2> </td> </tr> <tr> <td><b><center>Ends: </center></b></td><td align=center><?php edateselect();?>    <?php etimeselect();?>        </td> </tr> <tr> <td colspan=2> </td> </tr> <tr> <tr> <td><center><b>Category: </b></center></td> <td><select name='event'> <option value='Music'>Music <option value='Sports'>Sports <option value='Shows'>Shows <option value='LocalFestivs'>LocalFestivs <option value='other'>Other</select> </td> </tr> <input type="hidden" name="do" value="Submit"> <tr> <td colspan='2' align="center"> <input type="submit" name="do" value="Submit"> </td> </tr> </table> </form> </center> </body> </html> Quote Link to comment Share on other sites More sharing options...
haku Posted January 23, 2009 Share Posted January 23, 2009 Your code looks ok, so I would suggest changing the 'onsubmit' in the form to 'onclick' on the submit button. See how that works. Quote Link to comment Share on other sites More sharing options...
glenelkins Posted January 23, 2009 Share Posted January 23, 2009 or change the onSubmit to: onSubmit="return ValidateFields();" Now when your function returns False, as in the fields are not correct, the form will not submit Quote Link to comment Share on other sites More sharing options...
haku Posted January 23, 2009 Share Posted January 23, 2009 Ahh of course. I never use inline event handlers, so that one went right over my head. Doing what I suggested would have ended up with the same result of the page still moving onwards. Quote Link to comment Share on other sites More sharing options...
dadamssg Posted January 23, 2009 Author Share Posted January 23, 2009 that worked except now they won't store into my session variables...any clue why? Quote Link to comment Share on other sites More sharing options...
valtido Posted January 23, 2009 Share Posted January 23, 2009 you will need session_start() at the top of the page if u are using php. Quote Link to comment Share on other sites More sharing options...
dadamssg Posted January 24, 2009 Author Share Posted January 24, 2009 i put the session_start(); function at the type..same result, redisplayed the form with my error messages...anybody got a clue? Quote Link to comment Share on other sites More sharing options...
haku Posted January 25, 2009 Share Posted January 25, 2009 For a new topic you should start a new thread, and post the relative code. Quote Link to comment 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.