I use the following PHP mailer script that pyro made (hope it looks ok!?)
<?PHP
$msg = "";
if (empty($_POST['NAME'])) {
$msg .= "Your name wasn't entered.<br>";
}
if (!empty($msg)) {
include "/path/to/header.php"; # include your header
echo "<p>$msg</p>";
include "/path/to/footer.php"; # include your footer
exit();
}
#######################################################
# This script is Copyright 2003, Infinity Web Design #
# Distributed by http://www.webdevfaqs.com #
# Written by Ryan Brill - ryan@infinitypages.com #
# All Rights Reserved - Do not remove this notice #
#######################################################
## The lines below need to be edited...
###################### Set up the following variables ######################
#
$to = "me@domain.com"; #set address to send form to
$subject = "Submission."; #set the subject line
$headers = "From: someone@someone.com"; #set the from address, or any other headers
$forward = 1; # redirect? 1 : yes || 0 : no
$location = "http://domain.com/thanks/index.php"; #set page to redirect to, if 1 is above
#
##################### No need to edit below this line ######################
## set up the time ##
$date = date ("l, F jS, Y");
$time = date ("h:i A");
## mail the message ##
$msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n";
$msg .= "IP: ".$_SERVER['REMOTE_ADDR']."\n\n";
mail($to, $subject, $msg, $headers);
if ($forward == 1) {
header ("Location:$location");
}
else {
echo "Thank you for submitting our form. We will get back to you as soon as possible.";
}
?>
and use it from my actual forms by using:
<form action="mailer.php" method="post">
This works fine, however I wondered it it was possible for the mailer to be within the actual form file itself.
Let's say my form is a.php, when a.php is submitted, it then shows "mailer.php", I basically want it to submit to itself and then find any errors, and if something hasn't been entered and is validated, the errors to show in the actual form page itself (instead of a new page as it is now) and if everything is ok, to submit.
I tried putting the code above in my actual form, but it went horribly wrong, as soon as requested the form errors were shown, and so forth.
Any ideas on how I can achieve this please?
Any help is much appreciated.
Regards,
Webfreak
05-04-2004, 06:43 AM
Putting php script on same page isn't so complicated. I was trying to do same thing :rolleyes: Just paste script where you want to display message and change this: <form action="mailer.php" method="post"> to this <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
This should work :p
DanUK
05-04-2004, 12:46 PM
Hiya,
Thanks for the reply.
Unfortunately it didn't work. Basically when I loaded the page, the errors were shown as soon as the page loaded instead of when it was submitted, and when there's an error, it includes header.php/footer.php the design went mad, and i had two of everything almost!
Very weird. Hmmm...
Regards,
Webfreak
05-04-2004, 12:49 PM
Can you post here a errors?
EDIT: And/or a link
DanUK
05-04-2004, 01:20 PM
Hello,
Thanks for the response.
It's on a LAN so I cannot provide a link, sorry.
By errors, I mean the validation errors, i.e. the "Your name wasn't entered."
These show as soon as the page is loaded, instead of when the form is submitted, and as the mailer also includes header and footer, I guess I should remove this, as this is what was causing the other issues.
Hey RefreshF5, Thanks.
Sorry if I didn't understand properly, but would it look something like:
<?php
include('header.php');
?>
My form HTML here.
<?php
include('footer.php');
$submt=$_POST['submit'];
if(isset($submit))
{
$msg = "";
if (empty($_POST['NAME'])) {
$msg .= "Your name wasn't entered.<br>";
}
if (!empty($msg)) {
include "/path/to/header.php"; # include your header
echo "<p>$msg</p>";
include "/path/to/footer.php"; # include your footer
exit();
}
##################################################
#####
# This script is Copyright 2003, Infinity Web Design #
# Distributed by <a href="http://www.webdevfaqs.com" target="_blank">http://www.webdevfaqs.com</a> #
# Written by Ryan Brill - ryan@infinitypages.com #
# All Rights Reserved - Do not remove this notice #
##################################################
#####
## The lines below need to be edited...
###################### Set up the following variables ######################
#
$to = "me@domain.com"; #set address to send form to
$subject = "Submission."; #set the subject line
$headers = "From: someone@someone.com"; #set the from address, or any other headers
$forward = 1; # redirect? 1 : yes || 0 : no
$location = "http://domain.com/thanks/index.php"; #set page to redirect to, if 1 is above
#
##################### No need to edit below this line ######################
## set up the time ##
$date = date ("l, F jS, Y");
$time = date ("h:i A");
## mail the message ##
$msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n";
$msg .= "IP: ".$_SERVER['REMOTE_ADDR']."\n\n";
mail($to, $subject, $msg, $headers);
if ($forward == 1) {
header ("Location:$location");
}
else {
echo "Thank you for submitting our form. We will get back to you as soon as possible.";
}
}
else
{
//do nothing
}
?>
ShrineDesigns
05-04-2004, 03:25 PM
or you could just test for the $_POST
example:<?php
if($_POST)
{
echo "you did post";
}
else
{
echo "you didn't post";
}
?>
Webfreak
05-05-2004, 05:50 AM
Finnaly i repair the script. This should work, because i tested it.
<?PHP
if ($_SERVER['REQUEST_METHOD'] == "POST"){
if (empty($_POST['NAME'])) {
$msg .= "Your name wasn't entered.<br>";
}
if (!empty($msg)) {
include "/path/to/header.php"; # include your header
echo "<p>$msg</p>";
include "/path/to/footer.php"; # include your footer
exit();
}
##################################################
#####
# This script is Copyright 2003, Infinity Web Design #
# Distributed by http://www.webdevfaqs.com #
# Written by Ryan Brill - ryan@infinitypages.com #
# All Rights Reserved - Do not remove this notice #
##################################################
#####
## The lines below need to be edited...
###################### Set up the following variables ######################
#
$to = "me@domain.com"; #set address to send form to
$subject = "Submission."; #set the subject line
$headers = "From: someone@someone.com"; #set the from address, or any other headers
$forward = 0; # redirect? 1 : yes || 0 : no
$location = "http://domain.com/thanks/index.php"; #set page to redirect to, if 1 is above
#
##################### No need to edit below this line ######################
## set up the time ##
$date = date ("l, F jS, Y");
$time = date ("h:i A");
## mail the message ##
$msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n";
$msg .= "IP: ".$_SERVER['REMOTE_ADDR']."\n\n";
mail($to, $subject, $msg, $headers);
if ($forward == 1) {
header ("Location:$location");
}
else {
echo "Thank you for submitting our form. We will get back to you as soon as possible.";
}
}
?>
You can check out this here (http://193.95.194.177/form.php).
DanUK
05-05-2004, 06:17 AM
Ah thank you!!!
So I put like:
<?php
include('header.php');
if ($_SERVER['REQUEST_METHOD'] == "POST"){
if (empty($_POST['NAME'])) {
$msg .= "Your name wasn't entered.<br>";
}
#####
# This script is Copyright 2003, Infinity Web Design #
# Distributed by <a href="http://www.webdevfaqs.com" target="_blank">http://www.webdevfaqs.com</a> #
# Written by Ryan Brill - ryan@infinitypages.com #
# All Rights Reserved - Do not remove this notice #
##################################################
#####
## The lines below need to be edited...
###################### Set up the following variables ######################
#
$to = "me@domain.com"; #set address to send form to
$subject = "Submission."; #set the subject line
$headers = "From: someone@someone.com"; #set the from address, or any other headers
$forward = 0; # redirect? 1 : yes || 0 : no
$location = "http://domain.com/thanks/index.php"; #set page to redirect to, if 1 is above
#
##################### No need to edit below this line ######################
## set up the time ##
$date = date ("l, F jS, Y");
$time = date ("h:i A");
## mail the message ##
$msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n";
$msg .= "IP: ".$_SERVER['REMOTE_ADDR']."\n\n";
mail($to, $subject, $msg, $headers);
if ($forward == 1) {
header ("Location:$location");
}
else {
echo "Thank you for submitting our form. We will get back to you as soon as possible.";
}
Right?
So the only "new" line is if ($_SERVER['REQUEST_METHOD'] == "POST"){?
Regards,
Webfreak
05-05-2004, 06:31 AM
Yes this is the only line but don't forget on } on the end of script ;)
DanUK
05-05-2004, 09:54 AM
Hiya,
Okay I think we're getting there :)
The only problem now is, because I have "multiple" validations, I used the below code:
<?php
include('header.php');
if ($_SERVER['REQUEST_METHOD'] == "POST"){
$msg = "";
if (empty($_POST['NAME'])) {
$msg .= "Your name must be entered.<br>";
}
if (!preg_match("/^[a-zA-Z0-9]+$/", $USER)) {
$msg .= "Your user is invalid or not entered.<br>";
}
if (!preg_match('/[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+/', $_POST['EMAIL'])) {
$msg .= "Your email is invalid.<br>";
}
if (!empty($msg)) {
echo "<p>$msg</p>";
exit();
}
#######################################################
# This script is Copyright 2003, Infinity Web Design #
# Distributed by http://www.webdevfaqs.com #
# Written by Ryan Brill - ryan@infinitypages.com #
# All Rights Reserved - Do not remove this notice #
#######################################################
## The lines below need to be edited...
###################### Set up the following variables ######################
#
$to = "someone@someone.com"; #set address to send form to
$subject = "Submission."; #set the subject line
$headers = "From: someone@someone.com"; #set the from address, or any other headers
$forward = 1; # redirect? 1 : yes || 0 : no
$location = "http://www.someonedomain.com/thankyou/index.php"; #set page to redirect to, if 1 is above
#
##################### No need to edit below this line ######################
## set up the time ##
$date = date ("l, F jS, Y");
$time = date ("h:i A");
## mail the message ##
$msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n";
$msg .= "IP: ".$_SERVER['REMOTE_ADDR']."\n\n";
mail($to, $subject, $msg, $headers);
if ($forward == 1) {
header ("Location:$location");
}
else {
echo "Thank you for submitting our form. We will get back to you as soon as possible.";
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
The HTML form here.
</form>
<?php
include('footer.php');
?>
The problem is that when the script is submitted, the errors are correctly shown if something wasn't filled in - i.e. the "Your name must be entered", but when these are shown - it doesn't load the rest of the form, I Think this is because exit(); is there.
Did I do something wrong?
Thanks again.
Regards,
Webfreak
05-05-2004, 01:35 PM
Ok. This is really caused by exit() tag. You can do something else.
Try this:<?php
include('header.php');
if ($_SERVER['REQUEST_METHOD'] == "POST"){
$msg = "";
if (empty($_POST['NAME'])) {
$msg .= "Your name must be entered.<br>";
}
if (!preg_match("/^[a-zA-Z0-9]+$/", $USER)) {
$msg .= "Your user is invalid or not entered.<br>";
}
if (!preg_match('/[^x00-x20()<>@,;:\".[]x7f-xff]+(?:.[^x00-x20()<>@,;:\".[]x7f-xff]+)*@[^x00-x20()<>@,;:\".[]x7f-xff]+(?:.[^x00-x20()<>@,;:\".[]x7f-xff]+)+/', $_POST['EMAIL'])) {
$msg .= "Your email is invalid.<br>";
}
if (!empty($msg)) {
echo "<p>$msg</p>";
} else {
##################################################
#####
# This script is Copyright 2003, Infinity Web Design #
# Distributed by <a href="http://www.webdevfaqs.com" target="_blank">http://www.webdevfaqs.com</a> #
# Written by Ryan Brill - ryan@infinitypages.com #
# All Rights Reserved - Do not remove this notice #
##################################################
#####
## The lines below need to be edited...
###################### Set up the following variables ######################
#
$to = "someone@someone.com"; #set address to send form to
$subject = "Submission."; #set the subject line
$headers = "From: someone@someone.com"; #set the from address, or any other headers
$forward = 1; # redirect? 1 : yes || 0 : no
$location = "http://www.someonedomain.com/thankyou/index.php"; #set page to redirect to, if 1 is above
#
##################### No need to edit below this line ######################
## set up the time ##
$date = date ("l, F jS, Y");
$time = date ("h:i A");
## mail the message ##
$msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n";
$msg .= "IP: ".$_SERVER['REMOTE_ADDR']."\n\n";
mail($to, $subject, $msg, $headers);
if ($forward == 1) {
header ("Location:$location");
}
else {
echo "Thank you for submitting our form. We will get back to you as soon as possible.";
}
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Your HTML form.
</form>
<?php
include('footer.php');
?>
If message is empty then send mail else
don't send mail but show the form. Simple :p
webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved.