mraza Posted October 5, 2009 Share Posted October 5, 2009 Hi please somebody can help me in this. I have two process for this form you can see on the end of the page that after submitting where they move. now i need to validate this form and i am not sure what to do. Please any help. Thanks <div id="form" style="margin: 90px;"> <?php if $_POST['submit'] { $title = $_POST['title']; $name = $_POST['name']; $insurance = $_POST['insurance']; $address = $_POST['address']; $phone = $_POST['phone']; $email = $_POST['title']; $error = ""; if (!$title) $error = $error. "Title <br />" if ($name) $error = $error. "Name <br />" if (!$insurance) $error = $error. "National Insurance Number <br />" if (!$address) $error = $error. "Address <br />" if (!$phone) $error = $error. "Telephone Number <br />" if (!$email) $error = $error. "Email Address <br />" if (!$error !="") echo "Please Fill in The Following Required Fields <br /> $error"; else { return $_POST['submit']; } } ?> <form action="" method="POST"> <table> <tr> <td align="right"> Title:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="title"> </td> </tr> <tr> <td align="right"> Name:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="name"> </td> </tr> <tr> <td align="right"> National Insurance<br /> Number:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="insurance"> </td> </tr> <tr> <td align="right"> Address:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="address"> </td> </tr> <tr> <td align="right"> Tleephone Number:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="phone"> </td> </tr> <tr> <td align="right"> Email:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="email"> </td> </tr> <tr> <td align="right"> CSCS Registration Number: </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="registration"> </td> </tr> <tr> <td align="right"> Special Accommodations: </td> <td> <textarea style="border:1px solid #4088b8;" type="textarea" cols="30" rows="5" name="comments"> </textarea> </td> </tr> <tr><td> </td></tr> <tr><td> </td> <td><input type="button" value="Payment"onClick="this.form.action='payments.php';this.form.submit()" /> <input type="button" value="More Information" onClick="this.form.action='confirmation.php';this.form.submit()" /></td> </tr> </table> </form> </div> Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/ Share on other sites More sharing options...
mraza Posted October 5, 2009 Author Share Posted October 5, 2009 sorry i posted the wrong one here is the code what i need to know what should i put in $_POST['submit']. because i am getting this error: Notice: Undefined index: submit in C:\Program Files\wamp\www\site\includes\registration.php on line 25 Thanks for any help <?php if ($_POST['submit']) { $title = $_POST['title']; $name = $_POST['name']; $insurance = $_POST['insurance']; $address = $_POST['address']; $phone = $_POST['phone']; $email = $_POST['title']; $error = ""; if (!$title) $error = $error. "Title <br />"; if (!$name) $error = $error. "Name <br />"; if (!$insurance) $error = $error. "National Insurance Number <br />"; if (!$address) $error = $error. "Address <br />"; if (!$phone) $error = $error. "Telephone Number <br />"; if (!$email) $error = $error. "Email Address <br />"; if (!$error !="") echo "Please Fill in The Following Required Fields <br /> $error"; else { return $_POST['submit']; } } ?> <div id="form" style="margin: 90px;"> <form action="" method="POST"> <table> <tr> <td align="right"> Title:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="title"> </td> </tr> <tr> <td align="right"> Name:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="name"> </td> </tr> <tr> <td align="right"> National Insurance<br /> Number:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="insurance"> </td> </tr> <tr> <td align="right"> Address:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="address"> </td> </tr> <tr> <td align="right"> Tleephone Number:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="phone"> </td> </tr> <tr> <td align="right"> Email:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="email"> </td> </tr> <tr> <td align="right"> CSCS Registration Number: </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="registration"> </td> </tr> <tr> <td align="right"> Special Accommodations: </td> <td> <textarea style="border:1px solid #4088b8;" type="textarea" cols="30" rows="5" name="comments"> </textarea> </td> </tr> <tr><td> </td></tr> <tr><td> </td> <td><input type="button" value="Payment"onClick="this.form.action='payments.php';this.form.submit()" /> <input type="button" value="More Information" onClick="this.form.action='confirmation.php';this.form.submit()" /></td> </tr> </table> </form> </div> Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-930995 Share on other sites More sharing options...
cags Posted October 5, 2009 Share Posted October 5, 2009 Technically 'Undefined index' is not an error it's a notice, but thats by the by. It basically means it's tried to access an item in the $_POST array with a key of 'submit' and one doesn't exist. The way to avoid this notice is to check if the variable exists. <?php if(isset($_POST['submit']) { // etc ?> Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931002 Share on other sites More sharing options...
mraza Posted October 5, 2009 Author Share Posted October 5, 2009 pleaes have a look at the last line i am not using submit method. i am moving this form to two diffrent pages .. <td><input type="button" value="Payment"onClick="this.form.action='payments.php';this.form.submit()" /> <input type="button" value="More Information" onClick="this.form.action='confirmation.php';this.form.submit()" /></td> now what should i write on place of if $_POST['submit'] { Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931010 Share on other sites More sharing options...
cags Posted October 5, 2009 Share Posted October 5, 2009 Well that would be considered bad practice in terms of accessibility, but if you wish to do it that way, just add a hidden input for submit. <input type="hidden" name="submit" value="1" /> Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931019 Share on other sites More sharing options...
mraza Posted October 5, 2009 Author Share Posted October 5, 2009 well i added the line as : <td><input type="hidden" name="submit" value="1" /><input type="button" value="Payment" onClick="this.form.action='payments.php';this.form.submit()" /> <input type="button" value="More Information" onClick="this.form.action='confirmation.php';this.form.submit()" /></td> and same error Notice: Undefined index: submit in C:\Program Files\wamp\www\cscssolutions\includes\registration.php on line 25 Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931031 Share on other sites More sharing options...
mraza Posted October 5, 2009 Author Share Posted October 5, 2009 i have tried this way to but no luck : if ($_POST['submit'] || $_POST['submit2'] ) { and in form: <td><input type="button" name="submit" value="Payment" onClick="this.form.action='payments.php';this.form.submit()" /> <input type="button" name="submit2" value="More Information" onClick="this.form.action='confirmation.php';this.form.submit()" /></td> <?php if ($_POST['submit'] || $_POST['submit2'] ) { $title = $_POST['title']; $name = $_POST['name']; $insurance = $_POST['insurance']; $address = $_POST['address']; $phone = $_POST['phone']; $email = $_POST['title']; $error = ""; if (!$title) $error = $error. "Title <br />"; if (!$name) $error = $error. "Name <br />"; if (!$insurance) $error = $error. "National Insurance Number <br />"; if (!$address) $error = $error. "Address <br />"; if (!$phone) $error = $error. "Telephone Number <br />"; if (!$email) $error = $error. "Email Address <br />"; if (!$error !="") echo "Please Fill in The Following Required Fields <br /> $error"; else { return $_POST['submit']; } } ?> <div id="form" style="margin: 90px;"> <form action="" method="POST"> <table> <tr> <td align="right"> Title:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="title"> </td> </tr> <tr> <td align="right"> Name:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="name"> </td> </tr> <tr> <td align="right"> National Insurance<br /> Number:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="insurance"> </td> </tr> <tr> <td align="right"> Address:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="address"> </td> </tr> <tr> <td align="right"> Tleephone Number:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="phone"> </td> </tr> <tr> <td align="right"> Email:<span style="color:red;">*</span> </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="email"> </td> </tr> <tr> <td align="right"> CSCS Registration Number: </td> <td> <input style="border:1px solid #4088b8;" type="text" size="29" name="registration"> </td> </tr> <tr> <td align="right"> Special Accommodations: </td> <td> <textarea style="border:1px solid #4088b8;" type="textarea" cols="30" rows="5" name="comments"> </textarea> </td> </tr> <tr><td> </td></tr> <tr><td> </td> <td><input type="button" name="submit" value="Payment" onClick="this.form.action='payments.php';this.form.submit()" /> <input type="button" name="submit2" value="More Information" onClick="this.form.action='confirmation.php';this.form.submit()" /></td> </tr> </table> </form> </div> Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931038 Share on other sites More sharing options...
cags Posted October 5, 2009 Share Posted October 5, 2009 Put this just above that check echo '<pre>'; print_r($_POST); echo '</pre>'; And see what values your $_POST array contains after you have clicked your submit button. Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931042 Share on other sites More sharing options...
mraza Posted October 5, 2009 Author Share Posted October 5, 2009 its giving me an empty array Array ( ) Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931046 Share on other sites More sharing options...
cags Posted October 5, 2009 Share Posted October 5, 2009 It says that after you click the button? If it does then it is not submitting the form properly. Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931050 Share on other sites More sharing options...
mraza Posted October 5, 2009 Author Share Posted October 5, 2009 no after click button form will move to "payments.php" or "confirmation.php" as i am using onClick method instead of submit. that empty array i got including at top of the code on same page. Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931051 Share on other sites More sharing options...
cags Posted October 5, 2009 Share Posted October 5, 2009 The whole point of the code is to show the contents of the $_POST array, which is how your looking at carrying over information. The code needs to be put on whichever page the form is submitting to. Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931054 Share on other sites More sharing options...
mraza Posted October 5, 2009 Author Share Posted October 5, 2009 oops sorry i missunderstood here is what i got: Array ( [title] => [name] => [insurance] => [address] => [phone] => => [registration] => [comments] => ) Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931056 Share on other sites More sharing options...
cags Posted October 5, 2009 Share Posted October 5, 2009 Ok, I just realised your button is called submit which would probably override the hidden element. Rename the hidden item to is_submit or something like that. Then re-run the code and see if is_submit appears in that list. Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931058 Share on other sites More sharing options...
mraza Posted October 5, 2009 Author Share Posted October 5, 2009 yes it appears: Array ( [title] => [name] => [insurance] => [address] => [phone] => => [registration] => [comments] => [is_submit] => 1 ) Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931060 Share on other sites More sharing options...
cags Posted October 5, 2009 Share Posted October 5, 2009 So based on your first post... if ($_POST['submit']) { Should now be if ($_POST['is_submit']) { Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931064 Share on other sites More sharing options...
mraza Posted October 5, 2009 Author Share Posted October 5, 2009 now i am getting this: Notice: Undefined index: is_submit in C:\Program Files\wamp\www\site\includes\registration.php on line 25 Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931065 Share on other sites More sharing options...
cags Posted October 5, 2009 Share Posted October 5, 2009 Look at the contents of the $_POST array and compare them to your form. Undefined index means the item isn't in the array, so on line 25 there is a variable your trying to use that isn't in the array. Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931066 Share on other sites More sharing options...
mraza Posted October 5, 2009 Author Share Posted October 5, 2009 when i submit the form and i can see is_submit in array with print_r($_POST); but its showing same error on first page for undefined index of is_submit which i used on line 25: if ($_POST['is_submit']) { and in from: <td><input type="hidden" name="is_submit" value="1" /><input type="button" value="Payment" onClick="this.form.action='payments.php';this.form.submit()" /> <input type="button" value="More Information" onClick="this.form.action='confirmation.php';this.form.submit()" /></td> Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931069 Share on other sites More sharing options...
cags Posted October 5, 2009 Share Posted October 5, 2009 Why are you using that line of code on the first page if you aren't submitting to the same page? That makes no sense. Either way the way to stop the notice is to use if(isset($_POST['is_submit'])) { Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931072 Share on other sites More sharing options...
mraza Posted October 5, 2009 Author Share Posted October 5, 2009 actually i want to display an error on same page if a user will not fill a required field Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931079 Share on other sites More sharing options...
cags Posted October 5, 2009 Share Posted October 5, 2009 Which won't work, because you are submitting to a different page. If you wish to display an error on the same page (without alot of messing about with sessions) you will have to self submit. Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931083 Share on other sites More sharing options...
mraza Posted October 5, 2009 Author Share Posted October 5, 2009 so there is no way i think with PHP for that kind ok...thanks for your help Cags i will use Javascript validation instead. Best Wishes Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931088 Share on other sites More sharing options...
mraza Posted October 5, 2009 Author Share Posted October 5, 2009 ok here is the problem again i can't even use javascript validation. because there is no submit button. I need to use two files to move to one if user will choose to pay or next just they submit the request: <td><input type="button" value="Payment" onClick="this.form.action='payments.php';this.form.submit()" /> <input type="button" value="More Information" onClick="this.form.action='confirmation.php';this.form.submit()" /></td> Please can somebody some good way to show a display error on same page i am not sure what to do i need to move to two pages so i cant use one submit button Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931116 Share on other sites More sharing options...
cags Posted October 5, 2009 Share Posted October 5, 2009 Like I said in my previous post simply make the page submit to itself. Once the input is validated you can use the header() function to redirect to whatever page you need to be on. Link to comment https://forums.phpfreaks.com/topic/176599-solved-how-to-validate-this-form/#findComment-931125 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.