Click to See Complete Forum and Search --> : PHP mail to form newbie!! help!!


omc1jz
06-29-2003, 01:42 AM
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..

Pyro's PHP Script (http://forums.webdeveloper.com/showthread.php?s=&threadid=9265&perpage=15&highlight=mail%20to%20form&pagenumber=1)

Im ok with dreamweaver MX and also basic html coding , where do i start..

Cheers in advance!!!

brendandonhue
06-29-2003, 06:50 AM
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.

omc1jz
06-29-2003, 07:47 AM
Sweet..Thanks for the start..

That all seems straight forward..

What is $headers = "From: Form Mailer"; #set the from address..?

Do i have to change this ..?

Also is there a referance for the html? so i can get an idea or should i use d/weaver..?

Cheers for your time!!!
Sorry for the newbie ignoramus questions..!!!!

Maty...

brendandonhue
06-29-2003, 08:01 AM
Leave $headers alone.

Yes, you can use dreamWeaver for the HTML if you want.

omc1jz
06-29-2003, 08:07 AM
Cheers..!!

I just made a form page in dreaamweaver MX and it all seemed to go sweet... with all the things u mentioned..

Now im about to upload this to my Doteasy ultra hosting .

Where should i upload sendmail.php to ? In my web space there are 3 main folders (there are few more) HTML,PERL,CGI..

Thanks Again!!!

Maty...

brendandonhue
06-29-2003, 08:08 AM
On most servers it doesn't matter which folder PHP files go in.

omc1jz
06-29-2003, 08:24 AM
Your an absoloute legend!!!!!!

It worked first go. ive been stuffing around all day trying to sort that out....

Is there a way that i can make that script so the person HAS to fill out the form? and they cant submit unless they have filled in all fieldsS????

Thanks very much it is very much appreciated!!!!!

Maty!!!!!:D

pyro
06-29-2003, 08:44 AM
Try something like this:

<?PHP

#######################################################
# This script is Copyright 2003, Infinity Web Design #
# 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 = "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";

foreach ($_POST as $key => $value) {
$msg .= ucfirst ($key) ." : ". $value . "\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.]

omc1jz
06-29-2003, 10:22 AM
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 ?

Thanks again for the script pyro it is awesome!!!

Maty...

pyro
06-29-2003, 01:59 PM
I wouldn't use dreamweaver to validate it... Write your own code (or rather, mine... :p) This will loop through all the form elements checking if they are blank, or if they only contain blank spaces:

<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:

<form name="myform" action="someaction.php" method="post" onsubmit="return validate();">