daveyclark Posted September 23, 2010 Share Posted September 23, 2010 Hi I have a form that I want to validate the List box so if it is set to the default value (blank) when "send" is clicked, I don't want the form to go to my asp page which sends the email, I want it to show the error message then stop. So far, my form does show the error message, but then carries on and calls the asp page. My php code is below: <HTML> <HEAD> <TITLE>New Street Request</TITLE> <link rel="stylesheet" type="text/css" href="stylesheet.css" /> <script LANGUAGE="JavaScript"> <!-- function ValidateForm(form){ ErrorText= ""; if ( form.RequestedBy.selectedIndex == 0 ) { alert ( "Please select your Name from the list." ); return false; } if (ErrorText= "") { form.submit() } } --> </script> </HEAD> <BODY> <?php $moacode = $_POST['moacode']; $con = mssql_connect("server","username","password"); if (!$con) { die('Could not connect: ' . mssql_error()); } mssql_select_db("database", $con); $result = mssql_query("SELECT moa,name,name2,addr1,addr2,addr3,addr4 FROM MOAS where MOA=$moacode"); echo "<form action='cdontsmailreplymoanew.asp' method='POST'>"; if (!mssql_num_rows($result)) { echo "Requested By: <SELECT NAME='RequestedBy'> <br/>"; echo "<OPTION SELECTED VALUE=''>"; echo "<OPTION VALUE='person1@email.com'>person1"; echo "<OPTION VALUE='person2@email.com'>person2"; echo "<OPTION VALUE='person3@email.com'>person3"; echo "</SELECT><br/>"; echo "Name Line 1: <input type='text' size='50' maxlength='30' name='name' value=''> <br/>"; echo "Name Line 2: <input type='text' size='50' maxlength='30' name='name2' value=''> <br/>"; echo "Address Line 1: <input type='text' size='50' maxlength='30' name='addr1' value=''> <br/>"; echo "Address Line 2: <input type='text' size='50' maxlength='30' name='addr2' value=''> <br/>"; echo "Address Line 3: <input type='text' size='50' maxlength='30' name='addr3' value=''> <br/>"; echo "Post Code: <input type='text' size='50' maxlength='7' name='postcode' value=''>"; echo "<input type='hidden' size='50' name='header' value='new'>"; echo "<input type='hidden' size='50' name='moa' value='$moacode' Readonly> <br/>"; echo "<input type='submit' value='Send' onSubmit='CheckSubmit()'>"; } else { while($row = mssql_fetch_array($result)) { echo "Requested By: <SELECT NAME='RequestedBy'> <br/>"; echo "<OPTION SELECTED VALUE=''>"; echo "<OPTION VALUE='person1@email.com'>person1"; echo "<OPTION VALUE='person2@email.com'>person2"; echo "<OPTION VALUE='person3@email.com'>person3"; echo "</SELECT><br/>"; echo "MOA Code: <input type='text' size='50' name='moa' value='".$row['moa']."' ReadOnly> <br/>"; echo "Name Line 1: <input type='text' size='50' maxlength='30' name='name' value='".$row['name']."'> <br/>"; echo "Name Line 2: <input type='text' size='50' maxlength='30' name='name2' value='".$row['name2']."'> <br/>"; echo "Address Line 1: <input type='text' size='50' maxlength='30' name='addr1' value='".$row['addr1']."'> <br/>"; echo "Address Line 2: <input type='text' size='50' maxlength='30' name='addr2' value='".$row['addr2']."'> <br/>"; echo "Address Line 3: <input type='text' size='50' maxlength='30' name='addr3' value='".$row['addr3']."'> <br/>"; echo "Post Code: <input type='text' size='50' maxlength='7' name='postcode' value='".$row['addr4']."'>"; echo "<input type='hidden' size='50' name='header' value='old'>"; echo "<input type='submit' value='Send' onClick='ValidateForm(this.form)'>"; } } echo "</form>"; mssql_close($con); ?> </BODY> </HTML> If i change the type of the "Send" button to type="button", the message shows and the form does not submit, but the form never submits at all then even if the form gets past the validation, whereas the way it is just now, shows the message, but carries on and calls the asp page. Obviously there is somthing wrong with my validation or the type of the send button, but not sure what the solution is. Any ideas anyone? Cheers in advance. Quote Link to comment https://forums.phpfreaks.com/topic/214175-form-not-validating/ Share on other sites More sharing options...
Omirion Posted September 23, 2010 Share Posted September 23, 2010 Use Ajax to validate the form. I don't have time to write out the code but this a good tutorial on form validation using ajax. http://www.devshed.com/c/a/AJAX/PHP-AJAX-Form-Validation/ Quote Link to comment https://forums.phpfreaks.com/topic/214175-form-not-validating/#findComment-1114473 Share on other sites More sharing options...
daveyclark Posted September 24, 2010 Author Share Posted September 24, 2010 Cheers for response I've looked at your suggestion, and really can't see a way to use that with my current code. Any more suggestions? Or anyone else? Quote Link to comment https://forums.phpfreaks.com/topic/214175-form-not-validating/#findComment-1114947 Share on other sites More sharing options...
daveyclark Posted September 24, 2010 Author Share Posted September 24, 2010 Finally solved this! Kept my original code and changed it slightly. removed the second part of the javascript and added an else return true onto first bit so new JS looks like: <script language='JavaScript' type='text/JavaScript'> <!-- function validate() { if( document.mailform.RequestedBy.value=='') { alert('Please choose your name from the dropdown!'); return false; } else { return true; } } //--> </script> Left the type of send button as submit and form as method=post but took out the onclick of the form and called the JS onsubmit of the form! Cheers Quote Link to comment https://forums.phpfreaks.com/topic/214175-form-not-validating/#findComment-1114951 Share on other sites More sharing options...
alejandro52 Posted September 24, 2010 Share Posted September 24, 2010 so the only thing you have to do to cancel the form from going to the next post page is to return false to the onsubmit function? Quote Link to comment https://forums.phpfreaks.com/topic/214175-form-not-validating/#findComment-1114966 Share on other sites More sharing options...
BlueSkyIS Posted September 24, 2010 Share Posted September 24, 2010 turn off javascript and form is submitted. I would add server-side validation. Quote Link to comment https://forums.phpfreaks.com/topic/214175-form-not-validating/#findComment-1115016 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.