TheJoey Posted September 4, 2009 Share Posted September 4, 2009 i have two pages of code, but i am told i can only submit one. So im unsure how to go about doing both. One is validation and the other is to save the validated information. <?php if (!isset($_POST['submitForm'])) { ?> <form action="form.php" method="POST"> <table> <tr> <td>Name:</td> <td><input type="text" name="name"></td> </tr> <tr> <td>Email:</td> <td><input type="text" name="email"></td> </tr> <tr> <td>Message:</td> <td><textarea name="mesg"></textarea></td> </tr> <tr> <td><input type="submit" name="SubmitForm" value="Send"></td> </tr> <form> <?php header("Location: save.php"); } else { echo "Form submitted!"; } ?> <?php // Function to display form function showForm($errorName=false,$errorEmail=false,$errorMesg=false){ if ($errorName) $errorTextName = "Please enter your name!"; if ($errorEmail) $errorTextEmail = "Please enter a valid email address!"; if ($errorMesg) $errorTextMesg = "Please leave a longer message!"; echo '<form action="form.php" method="POST"><table>'; // Display name field an error if needed echo '<tr><td>Name:</td><td><input type="text" name="name"></td></tr>'; if ($errorName) echo "<tr><td colspan='2'>$errorTextName</td></tr>"; // Display email field an error if needed echo '<tr><td>Email:</td><td><input type="text" name="email"></td></tr>'; if ($errorEmail) echo "<tr><td colspan='2'>$errorTextEmail</td></tr>"; // Display message field an error if needed echo '<tr><td>Message:</td><td><textarea name="mesg"></textarea></td></tr>'; if ($errorMesg) echo "<tr><td colspan='2'>$errorTextMesg</td></tr>"; echo '<tr><td><input type="submit" name="SubmitForm" value="Send"></td></tr>'; echo '<form>'; } if (!isset($_POST['SubmitForm'])) { showForm(); } else { //Init error variables $errorName = false; $errorEmail = false; $errorMesg = false; $name = isset($_POST['name']) ? trim($_POST['name']) : ''; $email = isset($_POST['email']) ? trim($_POST['email']) : ''; $mesg = isset($_POST['mesg']) ? trim($_POST['mesg']) : ''; if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) $errorEmail = true; if (strlen($name)<3) $errorName = true; if (strlen($mesg)<10) $errorMesg = true; // Display the form again as there was an error if ($errorName || $errorEmail || $errorMesg) { showForm($errorName,$errorEmail,$errorMesg); } else { echo 'Submission was success!'; } } ?> Save.php <?php $name = $_POST['name']; $email= $_POST['email']; $mesg = $_POST['mesg']; $fp = fopen("data.txt","a"); if(!$fp) { echo 'Error: Cannot open file.'; exit; } fwrite($fp, $name.":".$email.":".$mesg."\r\n"); fclose($fp); echo Added ?> Quote Link to comment https://forums.phpfreaks.com/topic/173083-solved-form-validation-saving-information/ Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 Validate, then save. No reason to use two scripts. Just cram it all together. Quote Link to comment https://forums.phpfreaks.com/topic/173083-solved-form-validation-saving-information/#findComment-912267 Share on other sites More sharing options...
TheJoey Posted September 4, 2009 Author Share Posted September 4, 2009 just by use of else? or just start a new <?php ?> tags on the same page Quote Link to comment https://forums.phpfreaks.com/topic/173083-solved-form-validation-saving-information/#findComment-912271 Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 You don't even need to seperate the PHP sections.. but the latter would work. Quote Link to comment https://forums.phpfreaks.com/topic/173083-solved-form-validation-saving-information/#findComment-912276 Share on other sites More sharing options...
TheJoey Posted September 4, 2009 Author Share Posted September 4, 2009 Ty got it working like a treat Quote Link to comment https://forums.phpfreaks.com/topic/173083-solved-form-validation-saving-information/#findComment-912282 Share on other sites More sharing options...
bundyxc Posted September 4, 2009 Share Posted September 4, 2009 Awesome. Please mark as "Solved". Quote Link to comment https://forums.phpfreaks.com/topic/173083-solved-form-validation-saving-information/#findComment-912284 Share on other sites More sharing options...
TheJoey Posted September 4, 2009 Author Share Posted September 4, 2009 Thanks champ i already did no need to stress Quote Link to comment https://forums.phpfreaks.com/topic/173083-solved-form-validation-saving-information/#findComment-912288 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.