• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 555
  • Last Modified:

Redirect after calling same page in PHP

Hello all experts.

I have the code below working 100%.

The page itself contains only one field where the user types in his/her email address.  Then that one field is sent to my email address.  

But I want the user to be taken to a particular page, say google.com, after this form is processed.

All of this happens on the same page and it needs to stay that way.

Any ideas will be greatly appreciated.

Thanx,

--d.

<?php

    $email_from = $_POST['email']; // required 

if (trim($_REQUEST['contact']) == "yes"){

    // EDIT THE 2 LINES BELOW AS REQUIRED
	$email_to = "my_email@mail.com";
	$email_subject = "New Email Address for Newsletter Subscription from the Website"; 

	$email_message .= "Email: ".$email_from."\n";

	// create email headers
	$headers = 'From: '.$email_from."\r\n".
	'Reply-To: '.$email_from."\r\n" .
	'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers);  


}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<HTML>
<HEAD>
	<TITLE>php</TITLE>

<style type="text/css">

*{padding:0;margin:0;}
html {
	margin: 0px;
	padding: 0px;
	font-family: Arial, Helvetica, san-serif;
	font-size: 12px;
}
body {
	background: #000 url(background_main.jpg) repeat-x center top;
}
img {
	border: none;
}
#page_wrapper {
	margin: 0px auto;
	margin-top: 40px;
	width: 1116px;
}

#center_div {
/*	border: solid 1px black; */
	position: fixed;
	left: 50%;
	top: 50%;
	z-index: 100;
	height: 150px;
	margin-top: -135px;
	width: 450px;
	margin-left: -225px;
/* background-color: white; */	
} 

#front_image {
	text-align:center;
}
	
#front_mailing_list .wrapper {
	background-color: #ffbe43;
	color: #000;
	padding: 9px;
	font-size: 14px;
	font-weight: bold;
	line-height: 28px;	
	border: solid 2px black;	
}
#front_mailing_list input.textbox {
	background-color: #fff;
	border: none;
	color: #754C24;
	font-size: 20px;
	font-weight: bold;
	height: 28px;
	width: 220px;
	padding: 0;
	margin: 0;
    vertical-align: bottom;	
}
#front_mailing_list input.submit {
	background-color: #754C24;
	border: none;
	color: #FFF;
	font-size: 12px;
	font-weight: bold;
	height: 28px;
	width: 200px;
	padding: 0;
	margin: 0;	
    vertical-align: bottom;		
}
#nothanks {
	text-align:right;
	color:#fff;
}
#nothanks a {
	text-decoration:none;
	color:#fff;
}
#nothanks a:hover {
	text-decoration:underline;
	color:#fff;
}
</style>


	<script type="text/javascript" language="javascript">
<!--
function validEmail(Paddress) {
	var Vre;
		var Vret;
		Vre = /^[a-zA-Z0-9_\-\.]+@([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,4}$/;
		Vret = Paddress.search(Vre);
		if(Vret == 0)
			{return true}
		else
			{return false}

}

function isblank(str) {
	return (!str || 0 === str.length);
}

function validate_email() {
			var all_ok = true;
			var err_msg = "";
			var fld = "";
			
	if (isblank(document.getElementById('email').value)) {
		all_ok = false;
		err_msg += "\n - Email cannot be left blank";
		fld='email';
	}else{
		if(!validEmail(document.getElementById('email').value)){
			all_ok = false;
			err_msg += "\n - Invalid Email Address";
			fld='email';
		}
	}
	
	
	if (all_ok == false){
		alert("Please fix the following: \n" + err_msg); 
		document.getElementById(fld).focus(); 
	}
	return all_ok;    
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }

	//-->
	</SCRIPT>

</HEAD>

<BODY onLoad="self.focus();document.front_form.email.focus()">


		<div id="center_div">

<div id="front_image"></div>

			<div id="front_mailing_list">

				<div class="wrapper">
				 
			  	  <form name="front_form" action="<?php echo $_SERVER['PHP_SELF']?>" onsubmit="return validate_email()" method="post">
  				  <input type="hidden" name="contact" value="yes">
					<p>Enter your e-mail address to receive our newsletter:</p>
					<span class="input"><input name="email" id="email" type="text" class="textbox" size="28" maxlength="75" title="email"/></span>&nbsp;<span class="input"><input type="submit" name="submit" class="submit" value="S u b s c r i b e  N o w" /></span>
			  	  </form>

	    		</div>  <!-- wrapper -->


			</div>  <!-- mailing_list -->

<div id="nothanks"><a href="http://google.com">No thanks, take me to the home page &gt;&gt;</a></div>

		</div> <!-- center_div -->

</BODY>
</HTML>

Open in new window

0
driven_13
Asked:
driven_13
  • 2
  • 2
1 Solution
 
nanharbisonCommented:
Can you have the page handle the form, and then at the very top have some code which redirects the page if a certain variable is set at the end of this if statement code:
if (trim($_REQUEST['contact']) == "yes") {

your code;

header('Location: http://www.google.com/');

}
0
 
driven_13Author Commented:
That worked fine.

Thanx for your help.

--d.
0
 
nanharbisonCommented:
Great, thanks for the points. For future reference, headers have to be set before any content is output to the page, so it works here because your if statement is at the top of the page, before any content is displayed.
0
 
driven_13Author Commented:
OK, understood.

Thanx....[:0)

--d.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now