Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change echo to actual page

Status
Not open for further replies.

Wulfgen

Technical User
Dec 31, 2004
283
US
I have this script which I use for a flash based site - its an email script that collects the posted info and forwards it to the appropriate email box - I cobbled this together with some help from this forum __ I'm not a php guy.

Now I'm using this same script from a basic html form (post) and would rather have it go to a specific page rather than echoing a sentence in a blank page -- is it possible to change the "echo" part of this script to display a page instead?

here is the
Code:
<?php
//Create a temporary name for accepting the variable sent by Flash
$name =  $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comment = $_POST['comment'];
//Some fields entered contained whitespace and we dont want them, so use function trim() to
//delete away whitespace
$name=trim($name);
$email=trim($email);
$phone=trim($phone);
$website=trim($website);

// We use StripSlashes to delete away unwanted backslashes '\'
$comment=StripSlashes($comment);

//We are going to include the website field, and company field insid the $comment here
$message ="Contact Name : $name\n";
$message .="Contact Email : $email\n";
$message .="Contact Phone : $phone\n";
$message .="Comments : $comment";

//Modified this to suit your email address
$sendto='name@site.com';

//Modified this for displaying the subject in your mail
$subject="Contact Form";

mail($sendto,$subject,$message,"From: $name <$email>") or die("Failure");
 //clear all variables
     $name='';
     $email='';
	 $phone='';
	 $comment='';
     $message='';
echo ("you have submitted successfully");

?>
 
if you haven't sent output to the browser yet, then you should be able to use header() to send a location redirect
Code:
header("Location:http//[URL unfurl="true"]www.site.com/some/other/page.html")[/URL]

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
So I simply replace the echo statement with the header? then?
 
Hey jeff

worked like a charm -- but -- eee -- I just noticed that it sends a form no matter what -- there was an email check in the flash file, but not here, How can it be added, or can it -- remember i'm just getting into this php stuff
 
Suggestion:

Put the mail sending portion within an if statement that checks if something was entered in the email field.

In general:
It is prudent to validate the user input before taking an action like sending mail. User input should not be trusted without validation.

Code:
if (isset($_POST['email'])){
   # your code here
   # and think about validation please
}
header("Location: .....
 
So how would I validate before before submit - your suggestion about the if statement leaves me with even more questions - especially since I dont know a lot about php.

Its frustrating you know, just as you get somewhat proficient in one script/language, you then have to learn another, then after the learning curve, another..... yiikes!
 
Here's the idea:

When the page is initially accessed the $_POST['email'] will not exist. If that's the case, just go to the page.

When the page posts to itself the $_POST['email'] value will exist, then you can validate it, send the mail etc.

If you are used to conditional structures (maybe in ActionScript) then there's not much new here for you to learn. It's the same principle of conditional execution of code.
 
Ok so I tried this... it send that data but goes to a blank page - even if nothing is placed in the fields -- I've been up and down Google and Yahoo all day wih this -- and I'm getting nowhere -- I've cobbled the bit below from testing many,many scripts out there.....

PHP resources all seem to have this: result true / result false thing but not a word on sending a user to a success page or whatnot


Code:
<?php
//Create a temporary name for accepting the variable sent by Flash
$name =  $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comment = $_POST['comment'];
//Some fields entered contained whitespace and we dont want them, so use function trim() to
//delete away whitespace
$name=trim($name);
$email=trim($email);
$phone=trim($phone);
$comment=trim($comment);

// We use StripSlashes to delete away unwanted backslashes '\'
$comment=StripSlashes($comment);

//We are going to include the website field, and company field insid the $comment here
$message ="Contact Name : $name\n";
$message .="Contact Email : $email\n";
$message .="Contact Phone : $phone\n";
$message .="Comments : $comment";

//Modified this to suit your email address
$sendto='name@site.com';

//Modified this for displaying the subject in your mail
$subject="Contact Form";

mail($sendto,$subject,$message,"From: $name <$email>") or die("Failure");
 //clear all variables
     $name='';
     $email='';
     $phone='';
     $comment='';

if(isset($todo) and $todo=="test"){ 
 if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){ 
 header("Location:oops.htm"); 
 }elseheader("Location:contact_submit.htm"); 
 } 
?>
 
Hey Jeff,

hows this; I put this together from about three different sources -- the config.php just holds the email address and i got the email validation from php.net -- now all I have to do is figure out how the phone number accepts numbers only and have it use either radio or checkboxes - its coming along though


Code:
<?php
extract($HTTP_GET_VARS);
extract($HTTP_POST_VARS);
if ($action == "send")
{
include("config.php");
$to = $ademail;
$subject = $_POST['subject'];
$name = $_POST['fullname'];
$from = $_POST['from'];
$phone = $_POST['phone'];
$message = $_POST['message'];

$to = trim($to);
$subject = trim($subject);
$name = trim($name);
$from = trim($from);
$phone = trim($phone);
$message = trim($message);

//name error
if (empty($name))
{
$ermessage = "Error:\nPlease enter your name";
include("email_error.htm"); exit();
}
if (empty($to))
{
$ermessage = "Error:\nEmail address to cannot be blank,\nPlease enter your email address";
include("email_error.htm"); exit();
}

//email error
if (empty($from))
{
$ermessage = "Error:\nEmail address cannot be blank.\nPlease enter your email address";
include("email_error.htm"); exit();
}
if (!ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.
'@'.
'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $from))
{
$ermessage = "Error:\nInvalid Email address.\nPlease re-enter your email address!";
include ("email_error.htm"); exit;
}

//phone error
if (empty($phone))
{
$ermessage = "Error:\nPlease enter your Phone Number";
include("email_error.htm"); exit();
}

//Message error
if (empty($subject))
{
$ermessage = "Error:\nSubject cannot be blank.\nPlease enter email subject";
include("email_error.htm"); exit();
}
if (empty($message))
{
$ermessage = "Error:\nMessage area cannot be blank.\nPlease leave a message";
include("email_error.htm"); exit();
}


//email contents
$message = "\nThis message is from:\n  $from\n_______________________________________\n\nMy phone number is:\n  $phone\n_______________________________________\n\nMy message is:\n  $message\n_______________________________________\n\n+ + + + + + + + + + + + + + + + + + + +";
$send = mail($to, $subject, $message, "From: {$from}");
if ($send)
{
include("email_sent.htm"); exit();
}
else 
{ 
$ermessage = "Error: Your message has not been sent, please try again";
include("email_error.htm"); exit();
}
}
else
{
include("contact.html");
}
?>
 
The script is improving! You are getting somwhere.
As for the phone number only accepting numbers:
People like to use parantheses for area codes and hyphens. Here are two regex:
Code:
# Pattern A
$pattern = '/[^\d-\(\) ]/';
if(preg_match($pattern,$test,$foo)){
   # has disallowed characters
   # your code here
}

# Pattern B
$pattern = '/[^\d]/';
$test would hold the phone number
Pattern A:
Checks if there are that are not
- digit chars
- hyphens
- () parentheses
- or space

Pattern B (numbers only):
Checks if any non digit chars are in the variable

One more thing:
The extract ($HTTP_POST_VARS) is not advisable. Especially in light of the recent code injection bug with PHP versions below 4.4.1 and I believe 5.0.5 anything that emulates register_globals should be avoided.
It is sound practice to use the superglobal arrays $_POST and $_GET.

Hope that helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top