Hi all.. Just did a search and found pyro's PHP for mail to forms.. I would like to utilise this in my family's business website. My host supports PHP wich is a start..
I really need help setting up this script from scratch..
Its a VERY easy script, just copy and paste the PHP code into a file called "sendmail.php" or something like that.
Then you have to edit the variables
If your email address is bob@aol.com, the $to line would be like this:
$to = "bob@aol.com"; #set addres to send for to
If you had a page called thanks.html, to say thanks for sending you mail, you would set it in $loction:
$location = "thanks.html"; #set page to redirect to, if 1 is above
It will redirect to that page after the form is submitted. If you dont want a redirect, set $forward to a value of 0.
Save the file.
Create an HTML file with the form in it. Set the form's method to POST and the forms action to sendmail.php.
Create any textboxes, radio buttons, whatever else, and a submit button. THats it.
#######################################################
# This script is Copyright 2003, Infinity Web Design #
# Written by Ryan Brill - [email]ryan@infinitypages.com[/email] #
# All Rights Reserved - Do not remove this notice #
#######################################################
## The lines below need to be edited...
###################### Set up the following variables ######################
# #
$to = "you@your.com"; #set address to send form to
$subject = "Results from your Request Info form"; #set the subject line
$headers = "From: Form Mailer"; #set the from address
$forward = 1; # redirect? 1 : yes || 0 : no
$location = "thankyou.php"; #set page to redirect to, if 1 is above
# #
##################### No need to edit below this line ######################
$x = 0;
$message = "";
foreach ($_POST as $key => $value) { # loop through the POST vars
if ($value == "") { # if one is not filled out
$message .= "Please fill out the $key field.<br>"; #concatenate the message to the $message variable
$x = 1;
}
}
if ($x = 1) {
echo $message; # let users know which values need to be filled out.
}
else { # all required info is filled out
## 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";
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.");
}
}
?>
[edit - you should also combine this with a javascript that validates the form. Javascript is the best way to do it, as long as you have a server side backup for those who do not have javascript enabled.]
Cheers Pyro!
That script is a winner , I stuffed around with heaps of other scripts and yours worked flawlessly first time..
I actually came across it doing a google search for form mail and saw your comment regarding how unsecure it was.. so i investigated it further and it works a treat.. Thanks again!!
Could you send me in the right direction for the java to validate the form?
Dreamweaver MX can validate a form but i dont understand what is it doing, i ticked the required and email option but it does nothing when i test it on my server ?
I wouldn't use dreamweaver to validate it... Write your own code (or rather, mine... ) This will loop through all the form elements checking if they are blank, or if they only contain blank spaces:
Code:
<script type="text/javascript">
function validate(frm) {
msg = "Please fill out the following fields:\n";
x = 0;
for (i=0; i<frm.elements.length; i++) {
if(/^\s*$/.test(frm.elements[i].value)) {
msg += frm.elements[i].name+"\n";
x = 1;
}
}
if (x == 1) {
alert (msg);
return false;
}
}
</script>
Then, you will call it in your forms onsubmit handler, like this:
Bookmarks