PDA

View Full Version : Forms and email php issue


solid74
12-08-2009, 03:03 AM
Hey guys,

I'm new to the forum, and I was hoping to get a little assistance with my php script. I am failry new to Dreamweaver and completely new to php scripting.

I am setting up a simple page in Dreamweaver for a friend to collect data from potential customers. I have built the page and used spry validation form fields to collect the data. I just need to add functionality to the form itself. I did a bunch of reading and found some tutorial videos on youtube, but I am still having trouble grasping it.

I found a php script from one of the tutorials and was instructed to edit it to reflect the form in the tutorial. The problem though is that I have already made my form and I am pretty sure the php code does not reference the fields in my form. I attached the code in a text file.

I edited the email variables to reflect my php page and my friend's email address. I have two questions specifically. First, how to I edit the data variables section of the code to reflect my form fields on my page? And second, the bottom area of the code references a thank you message followed by a redirect to my friend's distributor's website. It doesn't seem to reference a seperate thank you page of any kind, how does it show the message? Does the script recode the webpage to display it or something? Am i using a completely wrong code?

Sorry if these are newbie questions, just not sure how to proceed and php is a completely new thing for me. I appreciate any assistance and thank you for reading. Please let me know if I need to provide any additional info, I attached the form html page as well! Thanks!!!

Jayson

edbr
12-08-2009, 03:46 AM
in the script there is this section

* Data Variables */

$email = $_POST['email'];
$name = $_POST['name'];
$comments = $_POST['comments'];
this is the data collected from the form. your form ahas these fields names
Rate
Comment1
Comment2
Comment3
Ref1
Ref2
Ref3
Name
Phone
you must first declare these like

$rate= $_POST['Rate'];
$Comment1= $_POST['Comment1'];
$Comment2= $_POST['Comment2'];
$Comment3= $_POST['Comment3'];

do the same with these

Ref1
Ref2
Ref3
Name
Phone and so on till you have the Post values names

as to the message this is the part that forms it
$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>

Comments: $comments <br>
EOD; you will have to alter it to reflect your variables
$body = <<<EOD
<br><hr><br>
Rate: $rate <br>
Comment1 :$Comment1 <br>
Comment2 :$Comment2 <br>
Comment3 :$Comment3 <br>
EOD; etc etc

solid74
12-08-2009, 04:41 AM
Thanks Edbr!

I am going to get to work and test it out, I will let you know how it goes. Thanks again!

Jayson

solid74
12-08-2009, 05:22 AM
So I got it all edited and saved, and I was trying to test it in browser preview. Is this something I can't do? When I enter data in all form fields and hit submit, it just reloads the page. No 'message sent' screen or redirect. I tested the spry validation and it worked when I didn't select a rating from the pull down. No email delivering form results either.

Thanks again for your help!

edbr
12-08-2009, 05:31 AM
you cant test this locally (unless you have an smtp serve on your computer) better to test it live

solid74
12-10-2009, 08:49 PM
Ok so I tested it on the server after I got my host all set up. It was partially working and I was receiving emails with only subject and phone. I redid my php a little because someone pointed out that I had a security issue with my header and someone could introduce a virus blast through the subject field. Anyway, I also decided to make a thank you page and even though I was getting email data, my thank you page wasn't coming up.

I worked on the code some more after doing some more research regarding syntax, and the form now submits succesfully and I get my thank you page to come up, but now I am getting no emails!!!

Here is my php code:

<?php
if ($_POST) {
$EmailTo = 'jayson.hickie@gmail.com';
$Subject = 'Comments from website';
$Rate = $_POST['Rate'];
$Comment1 = $_POST['Comment3'];
$Comment2 = $_POST['Comment3'];
$Comment3 = $_POST['Comment3'];
$Ref1 = $_POST['Ref1'];
$Ref2 = $_POST['Ref2'];
$Ref3 = $_POST['Ref3'];
$Name = $_POST['Name'];
$Email = $_POST['Email'];
$Phone = $_POST['Phone'];

$Body .= "Name:: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email:: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Phone:: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "Rate:: ";
$Body .= $Rate;
$Body .= "\n";
$Body .= "Comment1:: ";
$Body .= $Comment1;
$Body .= "\n";
$Body .= "Comment2:: ";
$Body .= $Comment2;
$Body .= "\n";
$Body .= "Comment3:: ";
$Body .= $Comment3;
$Body .= "\n";
$Body .= "Ref1:: ";
$Body .= $Ref1;
$Body .= "\n";
$Body .= "Ref2:: ";
$Body .= $Ref2;
$Body .= "\n";
$Body .= "Ref3:: ";
$Body .= $Ref3;
$Body .= "\n";

mail($EmailTo, $Subject, $Body);
header('Location: http://www.xldesignsonline.com/shakleevideo/thankyou.html');
}
?>


I will attach my html files as well if needed. Please can someone tell me what I am doing wrong?!? I am going crazy here lol

Thanks again for reading and for any advice!

solid74
12-11-2009, 05:14 AM
Hey there,

Ok, I simplified the code and now I am getting emails again and the thank you page comes up too. Only problem is the only info I get in the email is the phone number field from the form! The email subject shows up as well, but all my other form fields dont show up!

Here is the new code:

<?php
if ($_POST) {
$EmailTo = 'jayson.hickie@gmail.com';
$Subject = 'Comments from website';
$Body = $_POST['Rate'];
$Body = $_POST['Comment3'];
$Body = $_POST['Comment3'];
$Body = $_POST['Comment3'];
$Body = $_POST['Ref1'];
$Body = $_POST['Ref2'];
$Body = $_POST['Ref3'];
$Body = $_POST['Name'];
$Body = $_POST['Email'];
$Body = $_POST['Phone'];

$Mailsend = mail($EmailTo,$Subject,$Body);
header('Location: http://www.xldesignsonline.com/shakleevideo/thankyou.html');
}
?>


Please help!!! Thanks in advance :)

tux
12-11-2009, 11:43 PM
You are declaring the same variable each time ($body) so the script thinks that $Body is equal to the last item which is the phone number....

$Body = $_POST['Phone'];

Have a look at this code and see if you can use it for your needs. You will have to add any extra variables you need and add them to the email lower in the script.

You will notice there is two sections for your email, 1 for plain text and 1 for html.

See if you can understand it and let me know how you get on.

<?php
///////////////////////////////////////////////declare your variables here from the form on the previous page
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$rate = $_POST['rate'];
$subject = $_POST['subject'];
$comment1 = $_POST['comment1'];
// add your others here

///////////////////////////////////////////////ensure required fields are filled in
if($name && $email && $phone && $rate && $subject && $comment1){
///////////////////////////////////////////////
$to = 'your email address here';//define the receiver of the email
///////////////////////////////////////////////

//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: $email\r\nReply-To: $email";//The 'From' & 'Reply To' email address here
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<!--Place your code here for plain text email-->
Message from the Contact Form.\n\n
Name: <?php echo $name; ?>\n
Email Address: <?php echo $email; ?>\n
Phone: <?php echo $phone; ?>\n
Rate: <?php echo $rate; ?>\n\n
Your Comment: <?php echo $comment1; ?>

<!--------------------------------------->

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<!--Place your code here for HTML email-->
<p align="left" >Name: <b><?php echo $name; ?></b><br>
Email Address: <b><?php echo $email; ?></b><br>
Phone: <b><?php echo $phone; ?></b><br>
Rate: <b><?php echo $rate; ?></b></p>
<p>Your Comment: <b><?php echo $comment1; ?></b></p>

<!--------------------------------------->

--PHP-alt-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
header('Location: http://www.xldesignsonline.com/shakl...kyou.html');
exit;
}
?>

Regards, Paul