Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Redirect after calling same page in PHP

Posted on 2012-08-29
4
Medium Priority
?
551 Views
Last Modified: 2012-08-29
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
Comment
Question by:driven_13
  • 2
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
nanharbison earned 2000 total points
ID: 38347438
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
 

Author Comment

by:driven_13
ID: 38347461
That worked fine.

Thanx for your help.

--d.
0
 
LVL 17

Expert Comment

by:nanharbison
ID: 38347484
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
 

Author Comment

by:driven_13
ID: 38347493
OK, understood.

Thanx....[:0)

--d.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question