Joseph_R Posted May 31, 2013 Share Posted May 31, 2013 Hello to all php gurus, I'm new here and hoping someone kind can help me with a small issue on php form i have. 1st i would like is to echo a success message the same way the script echos error messages when one of the form feilds is empty. 2nd is i would like to do this by adding a peice of code where i want the success message to appear. For the error messages the script uses this piece of code here to echo the error messages where i want it to appear. "<? echo $error; ?>" if possible i want to use a similar code to echo the success message where i want it to appear, the full form code is below. <? // Attention! Please read the following. // It is important you do not edit pieces of code that aren't tagged as a configurable options identified by the following: // Configuration option. // Each option that is easily editable has a modified example given. $error = ''; $name = ''; $email = ''; //$phone = ''; Remove the // tags and this text to active phone number. $subject = ''; $comments = ''; $verify = ''; if(isset($_POST['contactus'])) { $name = $_POST['name']; $email = $_POST['email']; //$phone = $_POST['phone']; Remove the // tags and this text to active phone number. $subject = $_POST['subject']; $comments = $_POST['comments']; $verify = $_POST['verify']; // Configuration option. // You may change the error messages below. // e.g. $error = 'Attention! This is a customised error message!'; if(trim($name) == '') { $error = '<div class="error_message">Attention! You must enter your name.</div>'; } else if(trim($email) == '') { $error = '<div class="error_message">Attention! Please enter a valid email address.</div>'; // Configuration option. // Remove the // tags below to active phone number. //} else if(!is_numeric($phone)) { // $error = '<div class="error_message">Attention! Phone number can only contain digits.</div>'; } else if(!isEmail($email)) { $error = '<div class="error_message">Attention! You have enter an invalid e-mail address, try again.</div>'; } if(trim($subject) == '') { $error = '<div class="error_message">Attention! Please enter a subject.</div>'; } else if(trim($comments) == '') { $error = '<div class="error_message">Please enter your message.</div>'; } else if(trim($verify) == '') { $error = '<div class="error_message">Attention! Please enter the verification number.</div>'; } else if(trim($verify) != '4') { $error = '<div class="error_message">Attention! The verification number you entered is incorrect.</div>'; } if($error == '') { if(get_magic_quotes_gpc()) { $comments = stripslashes($comments); } // Configuration option. // Enter the email address that you want to emails to be sent to. // Example $address = "example@example.com"; $address = "example@example.com"; // Configuration option. // i.e. The standard subject will appear as, "You've been contacted by John Doe." // Example, $e_subject = '$name . ' has contacted you via Your Website.'; $e_subject = 'You\'ve been contacted by ' . $name . '.'; // Configuration option. // You can change this if you feel that you need to. // Developers, you may wish to add more fields to the form, in which case you must be sure to add them here. $e_body = "You have been contacted by $name with regards to $subject, their additional message is as follows.\r\n\n"; $e_content = "\"$comments\"\r\n\n"; // Configuration option. // RIf you active phone number, swap the tags of $e-reply below to include phone number. //$e_reply = "You can contact $name via email, $email or via phone $phone"; $e_reply = "You can contact $name via email, $email"; $msg = $e_body . $e_content . $e_reply; mail($address, $e_subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n"); // Email has sent successfully, echo a success page. echo "<div id='succsess_page'>"; echo "<em>Email Sent Successfully.</em>"; echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>"; echo "</div>"; } } if(!isset($_POST['contactus']) || $error != '') // Do not edit. { ?> Thanks in advance Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 31, 2013 Share Posted May 31, 2013 You should store the errors in an array, that way you can have more than one error at a time. Then if the errors array is empty and the form was submitted, you know it was successful. Quote Link to comment Share on other sites More sharing options...
boompa Posted May 31, 2013 Share Posted May 31, 2013 (edited) In other words, create an empty array to hold errors $errors = array(); your failed validations should add to this array like this $errors[] = 'This is an error'; and you check for errors with if (!empty($errors)) { Edited May 31, 2013 by boompa Quote Link to comment Share on other sites More sharing options...
Strider64 Posted May 31, 2013 Share Posted May 31, 2013 In other words, create an empty array to hold errors $errors = array(); your failed validations should add to this array like this $errors[] = 'This is an error'; and you check for errors with if (!empty($errors)) { Or yo could do this: <?php $errMsg[] = NULL; $errMsg[] = 'This error 1!'; $errMsg[] = 'This error 2!'; //print_r($errMsg); if ($errMsg) { foreach ($errMsg as $value) { echo '<p>' . $value . '</p>'; } } Quote Link to comment Share on other sites More sharing options...
Jessica Posted May 31, 2013 Share Posted May 31, 2013 You could, but that doesn't make it the best/right way. Quote Link to comment Share on other sites More sharing options...
Joseph_R Posted June 1, 2013 Author Share Posted June 1, 2013 (edited) Hello again, Let me start first by saying Thank You to Jessica, Boompa and Strider64 for replying kindly to help me but i am also new to php and I'm still at a lost. Can one of you put the full re-written php code for me along with the short code for me to place where i would like the success message to appear ? At the bottom of the form's submit button i used this code below to echo the error messages if a field is missing. This text shows up where i placed the code. <? echo $error; ?> When the form has been submitted succesfull it reloads the page showing the text from this part of the php code: // Email has sent successfully, echo a success page. echo "<div id='succsess_page'>"; echo "<em>Email Sent Successfully.</em>"; echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>"; echo "</div>"; But when it reloads it only shows part of my web page along with the success message, my menu and other parts of my web page do not show. Is there any way to get this success text showing where i placed the code for the error messages ? Edited June 1, 2013 by Joseph_R Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 1, 2013 Share Posted June 1, 2013 Can one of you put the full re-written php code for me along with the short code for me to place where i would like the success message to appear ? That's not what this forum is for. You want someone to do it for you, post in Freelancing and be ready to pay. Quote Link to comment Share on other sites More sharing options...
Joseph_R Posted June 5, 2013 Author Share Posted June 5, 2013 Hello again, I understand about not pasting the complete rewritten php code (god helps those who help them self) but none of you explain to me how to echo the success message only how to echo the error messages which is already functional in the code. All of my error messages echos correctly, its the success message i would like to echo instead of a success page. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 5, 2013 Share Posted June 5, 2013 I did explain to you. Logically, if you don't have an error, then you were successful. Otherwise, you need to define what successful means. As for echoing it as a "message" instead of a "page", are you saying you want the form to always display after submission? Quote Link to comment Share on other sites More sharing options...
teynon Posted June 5, 2013 Share Posted June 5, 2013 Or yo could do this: <?php $errMsg[] = NULL; $errMsg[] = 'This error 1!'; $errMsg[] = 'This error 2!'; //print_r($errMsg); if ($errMsg) { foreach ($errMsg as $value) { echo '<p>' . $value . '</p>'; } } @Strider64: if ($errMsg) the way you are using it is identical to if (!empty($errMsg)) except that yours will throw an error if it isn't set and yours is less descriptive about what is happening. We want code to represent what is happening logically, if at all possible and if (!empty($errMsg)) presents the most descriptive here. Additionally, I don't know why you would initialize an array with NULL. You should initialize arrays the correct way: $errMsg = array(); @Joseph_R You need to look into some PHP tutorials, as you clearly haven't tried to learn / figure out how to do this on your own. But, as you are clearly looking for a quick fix, change echo "<div id='succsess_page'>"; echo "<em>Email Sent Successfully.</em>"; echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>"; echo "</div>"; to $error .= "<div id='succsess_page'>"; $error .= "<em>Email Sent Successfully.</em>"; $error .= "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>"; $error .= "</div>"; Quote Link to comment Share on other sites More sharing options...
Joseph_R Posted June 6, 2013 Author Share Posted June 6, 2013 I did explain to you. Logically, if you don't have an error, then you were successful. Otherwise, you need to define what successful means. As for echoing it as a "message" instead of a "page", are you saying you want the form to always display after submission? Yes, but with a success message for example "thank you, your message has been submitted" After some headaches i managed to find a way to make my whole page's content show after a successful email was sent. I made a separate page with only the form then placed it on my main page using an iframe, when a successful email was sent it reloads with the success message and my whole page's content was shown. You were no help at all hahaha, just saying.... but i still appreciated the replies as some forums never reply to people until months after. Anyways Thanks again. Quote Link to comment Share on other sites More sharing options...
Joseph_R Posted June 6, 2013 Author Share Posted June 6, 2013 @Strider64: if ($errMsg) the way you are using it is identical to if (!empty($errMsg)) except that yours will throw an error if it isn't set and yours is less descriptive about what is happening. We want code to represent what is happening logically, if at all possible and if (!empty($errMsg)) presents the most descriptive here. Additionally, I don't know why you would initialize an array with NULL. You should initialize arrays the correct way: $errMsg = array(); @Joseph_R You need to look into some PHP tutorials, as you clearly haven't tried to learn / figure out how to do this on your own. But, as you are clearly looking for a quick fix, change echo "<div id='succsess_page'>"; echo "<em>Email Sent Successfully.</em>"; echo "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>"; echo "</div>"; to $error .= "<div id='succsess_page'>"; $error .= "<em>Email Sent Successfully.</em>"; $error .= "<p>Thank you <strong>$name</strong>, your message has been submitted to us.</p>"; $error .= "</div>"; One Million thanks to you Mr Teynon, This is what i wanted from the beginning and it works perfectly. I told Jessica that i had found a way to do it that so my full page contents are shown after a successful email but using iframes are not one of my favorite things. Thanks again Teynon Your my hero PROBLEM SOLVED (please close topic) Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 6, 2013 Share Posted June 6, 2013 You clearly didn't read the forums rules. 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.