Jump to content

if/else redirects after form submit...


artsyandi

Recommended Posts

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
}
?>

Link to comment
https://forums.phpfreaks.com/topic/260787-ifelse-redirects-after-form-submit/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.