artsyandi Posted April 12, 2012 Share Posted April 12, 2012 I am fairly new to PHP forms so I am trying to adapt an example that I found online. Right now it IS functioning... showing a error message OR showing a success message and sending all the form information to my email account. I would like to edit it to redirect to a specific page if it has an error (http://www.mosaleen.com/contact_error.html) and to another if it sends successfully (http://www.mosaleen.com/thankyou_email.html) I am a bit confused by how the script is looped at the end. However, if I delete out that last few lines of coding, the form stops working. I am attaching the code below. Keep in mind that I am a newbie! <?php // Turn on error reporting. Handy for debugging. error_reporting(E_ALL ^ E_NOTICE); // The following parameters are pretty much all that you // need to change except for the format of the email message // below. Note that your mail server may require the mailTo // address be in the host domain. $mailTo = "****"; // The address that will receive form submissions $mailSubject = "Comments from Mosaleen"; // Whatever you want $mailHost = "****"; // Usually looks like mail.yourhost.com $mailPort = "25"; // Usually 25 $mailAuth = true; // "true" if your mail server requires authentication, "false" if not $mailPassword = "****"; // The mail password associated with $mailTo // If you want to get cookies values... // $myCookie = $_COOKIE["cookiename"]; // Get the form fields. $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $message = $_POST['message']; // If there are errors, display them and a back button. if($errorMessages) { ?> <p>Errors were found on the form.</p> <ul> <?php echo $errorMessages; ?> </ul> <p><input type="button" value="Back" onClick="history.back()"></p> <?php } // No errors, send the message and print out success message. else { // These can sometimes be useful, although you should not // violate your site's privacy policy. $browser = $HTTP_USER_AGENT; $ip = $_SERVER['REMOTE_ADDR']; // Build the email.<br /> $body = " Name: $name Email: $email Phone: $phone Message: $message -------------------- Browser: $browser User IP: $ip"; include("Mail.php"); $headers["From"] = "[email protected]"; $headers["To"] = $mailTo; $headers["Subject"] = $mailSubject; $params["host"] = $mailHost; $params["port"] = $mailPort; $params["auth"] = $mailAuth; $params["username"] = $mailTo; $params["password"] = $mailPassword; $mail_object =& Mail::factory("smtp", $params); $mail_object->send($mailTo, $headers, $body); ?> <h1>Thank You</h1> <p>Thank you for contacting us. We will be in touch with you shortly.</p> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260787-ifelse-redirects-after-form-submit/ Share on other sites More sharing options...
artsyandi Posted April 12, 2012 Author Share Posted April 12, 2012 If anyone could help me clean up any unnecessary code, it would be helpful as well. Quote Link to comment https://forums.phpfreaks.com/topic/260787-ifelse-redirects-after-form-submit/#findComment-1336611 Share on other sites More sharing options...
caliux Posted April 12, 2012 Share Posted April 12, 2012 here you should write something else for redirect if($errorMessages) { ?> <p>Errors were found on the form.</p> <ul> header("Location: yourpage.php"); </ul> <p><input type="button" value="Back" onClick="history.back()"></p> <?php } Quote Link to comment https://forums.phpfreaks.com/topic/260787-ifelse-redirects-after-form-submit/#findComment-1336612 Share on other sites More sharing options...
artsyandi Posted April 12, 2012 Author Share Posted April 12, 2012 Thanks! I entered in your suggestion and made a few tweaks to clean it up and it is working now! I appreciate the help! Quote Link to comment https://forums.phpfreaks.com/topic/260787-ifelse-redirects-after-form-submit/#findComment-1336615 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.