Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

rookie email php help

Explorer ,
Dec 07, 2009 Dec 07, 2009

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 php code as well as the html file in question.

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?


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!  Thanks!!!

Jayson

TOPICS
Server side applications
644
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 08, 2009 Dec 08, 2009

First of all, the script that you found for processing form input and sending it by email is inherently unsafe. It inserts user input directly into the email headers, laying your page open to a malicious attack known as email header injection (Google it).

Sending user information from a form by email with PHP is basically very simple. Each form element must have a name. When the form is submitted, the user input in each form element can be retrieved in PHP variables that use the same name. If the form's method is set to POST (best for emails), the value of a field called "comments" is captured in $_POST['comments'], the value of a select menu called "Rate" is captured in $_POST['Rate'], and so on. PHP is case-sensitive, so $_Post['rate'] won't work. $_POST must be in uppercase; the value of the name must be exactly the same as in the form.

The action attribute of the form tells the browser where to find the processing script.

The following form sends two fields (name and comments) using the post method for processing by processmail.php:

<form id="form1" name="form1" method="post" action="processmail.php">
  <p>
    <label for="name">Name:</label>
    <input type="text" name="name" id="name" />
  </p>
  <p>
    <label for="comments">Comments:</label>
    <textarea name="comments" id="comments" cols="45" rows="5"></textarea>
  </p>
  <p>
    <input type="submit" name="send" id="send" value="Submit" />
  </p>
</form>

The processing script in processmail.php looks like this:

<?php
if ($_POST) {
     $to = 'me@example.com'; // email address to send the message to
     $subject = 'Comments from website';
     $message = 'Name: ' . $_POST['name'] . "\n";
     $message .= 'Comments: ' . $_POST['comments'];
     mail($to, $subject, $message);
     header('Location: http://www.example.com/thankyou.html');
}
?>

The message is built by joining the content of all the form fields into a single string of text. In PHP, a dot (or period) joins strings of text together. A dot in front of an equals sign (.=) adds the text to the existing value. "\n" (in double quotes) creates a new line.

All this is very elementary PHP, but if you don't understand PHP syntax, it probably looks like double-Dutch.

Try the form and processing script above, changing the email address and URL for the thank you page. Once you see how it works, it should be a lot easier for you to figure out how to adapt the script to your own page.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 08, 2009 Dec 08, 2009

David,

Thank you very much for your reply.  This has been a new beast for me to tame but once I get my brain around it I am hopeful it won't be that hard anymore.

I studied your code and I'm going to give it a try.  I have a few questions though.  First, can I modify your script to reflect my form fields?  Also, I saw the part of your code that indicates a thank you page.  I am assuming this comes up after the messgae is sent, if I build a thank you page can I still use the part of my code that will redirect it to another website?  Lastly, I am guessing I won't be able to test this locally with browser preivew right?

Thanks for the heads up regarding the sequrity issue, I would have never even thought about that.  I appreciate the advice and look forward to your reply!

Jayson

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 08, 2009 Dec 08, 2009
LATEST

Hey there, I just went ahead and gave it a shot, and I wanted to see if you could tell me if I am on the right track.

I edited the field id's in the php code to reflect my information but I am not sure if I am doing it right.  I will make a thank you page tonight but I'm still not sure how to do the redirect, I will try coding it in.  My friend is going to set up a host for this site tomorrow so I will be able to test it, I have heard that it won't work locally and that makes sense.

I attached the php and the html if you are interested in having a look.  I really appreciate any advice!  Thanks again!

Jayson

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines