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"]    = "contact@mosaleen.com";
	$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
Share on other sites

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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.