skter4938 Posted June 20, 2006 Share Posted June 20, 2006 Hello there.I have a simple php email form. The form itself contacts the php page called contact.php and contact.php sends the mail. I'm sure people who have made an email script before know what I am talking about.Well I have a validtaion section in the contact.php, and this is what it looks like:[code]$validationOK=true;if (checkEmail($EmailFrom) == FALSE){print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact/\">";exit;}else if (Trim($Name)==""){print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact/\">";exit;}else if (Trim($Subject)=="") {print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact/\">";exit;}if (Trim($Message)==""){print "<meta http-equiv=\"refresh\" content=\"0;URL=/contact/\">";exit;}else if (!$validationOK) {print "Email could not be sent due to errors. Please email the webmaster.";exit;}[/code]Well instead of just refreshing back to the contact page, I want it to display text that says for example "The email you entered is not valid" next to the email textbox on my form. How can I do that? Quote Link to comment https://forums.phpfreaks.com/topic/12470-validation-problems/ Share on other sites More sharing options...
.josh Posted June 20, 2006 Share Posted June 20, 2006 example:pagethatprocessesform.php[code]<?phpsession_start();$name = $_POST['name'];$email = $_POST['email'];// alter your conditions as you see fit for error checkingif (trim($name) == '') { $error_msg[] = "no name entered.";}if(trim($email) == '') { $error_msg[] = "no email entered.";}if ($error_msg) { $_SESSION['error_msg'] = $error_msg; header ('Location: pagewithformonit.php'); exit();else { //process email}?>[/code]pagewithformonit.php[code]<?phpsession_start();if ($_SESSION['error_msg']) { echo "the following problems occured:<br>"; foreach ($_SESSION['error_msg'] as $val) { echo $val . "<br>"; } unset($_SESSION['error_msg']);}//code to show the form?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12470-validation-problems/#findComment-47720 Share on other sites More sharing options...
skter4938 Posted June 20, 2006 Author Share Posted June 20, 2006 Thank you good sir [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /] But there is one small problemo....It gives me this error now[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/webtodes/public_html/contact/index.php:6) in /home/webtodes/public_html/contact/index.php on line 50[/quote]and line 50 is [code]session_start();[/code] on the form page. I tried taking it out, and it just doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/12470-validation-problems/#findComment-47817 Share on other sites More sharing options...
zq29 Posted June 20, 2006 Share Posted June 20, 2006 Is session_start(); called before [i]all[/i] page output? Quote Link to comment https://forums.phpfreaks.com/topic/12470-validation-problems/#findComment-47818 Share on other sites More sharing options...
skter4938 Posted June 20, 2006 Author Share Posted June 20, 2006 I put session_start(); right before the rest of the php code. Quote Link to comment https://forums.phpfreaks.com/topic/12470-validation-problems/#findComment-47819 Share on other sites More sharing options...
zq29 Posted June 20, 2006 Share Posted June 20, 2006 session_start(); needs to be called before any output (including HTML), its best to put it right at the top of your file, so that line 1 is "<?php session_start(); ?>". Quote Link to comment https://forums.phpfreaks.com/topic/12470-validation-problems/#findComment-47820 Share on other sites More sharing options...
skter4938 Posted June 20, 2006 Author Share Posted June 20, 2006 Ahhh Thank you :DEverything works. Except when it writes "Please enter an email...blah blah blah...." It refreshes the form. Is there anyway of it just erasing what is wrong? Quote Link to comment https://forums.phpfreaks.com/topic/12470-validation-problems/#findComment-47822 Share on other sites More sharing options...
.josh Posted June 21, 2006 Share Posted June 21, 2006 in your pagethatprocessesform.php you need to pass the variables back to the form, using sessions, just like the error_msg variableexample:pagethatprocessesform.php[code]<?phpsession_start();$name = $_POST['name'];$email = $_POST['email'];// alter your conditions as you see fit for error checkingif (trim($name) == '') { $error_msg[] = "no name entered.";}if(trim($email) == '') { $error_msg[] = "no email entered.";}if ($error_msg) { $_SESSION['error_msg'] = $error_msg; //create a session array with the values $_SESSION['form_values'] = array ('name' => $name, 'email' => $email); header ('Location: pagewithformonit.php'); exit();else { //process email}?>[/code]and then in pagewithformonit.php[code]<?phpsession_start();if ($_SESSION['error_msg']) { echo "the following problems occured:<br>"; foreach ($_SESSION['error_msg'] as $val) { echo $val . "<br>"; } unset($_SESSION['error_msg']);}?><!-- example of form --><form ...>Name: <input type = 'text' name = 'name' value='<?= $_SESSION['form_values']['name'] ?><br>Email: <input type = 'text' name = 'email' value='<?= $_SESSION['form_values']['email'] ?><br></form><?php unsest $_SESSION['form_values']; ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12470-validation-problems/#findComment-47914 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.