Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Mailing form data

From: Validating and Processing Forms with JavaScript and PHP

Video: Mailing form data

When you create a form to capture online data, the information you get usually has one of two destinations. The data gets sent to some email address where a human takes care of the request, or it's stored into a database. In this video, we'll look at what it takes to send an email with the result of your form. PHP has a built-in mail function which is perfect for sending the data. Here's a documentation page for mail in the php website. The mail function takes a series of strings with where to send the data, who to send it to, a subject, a message, and additional headers and parameters. PHP sends an email using the server's mail software. In Linux, this is usually called send mail.

Mailing form data

When you create a form to capture online data, the information you get usually has one of two destinations. The data gets sent to some email address where a human takes care of the request, or it's stored into a database. In this video, we'll look at what it takes to send an email with the result of your form. PHP has a built-in mail function which is perfect for sending the data. Here's a documentation page for mail in the php website. The mail function takes a series of strings with where to send the data, who to send it to, a subject, a message, and additional headers and parameters. PHP sends an email using the server's mail software. In Linux, this is usually called send mail.

If the server is able to send the email message, then the function returns true, otherwise, it returns false. The data, as well as additional headers, adheres to certain formats as dictated by the mail protocol. Additional headers can include traditional mail features like the from address, who to replyto, cc, etcetera. This type of mail command is probably one of the most attacked scripts on the internet, so make sure you take extra precaution validating and sanitizing your form input before you use this command.

This also means that your hosting company might be placing a limit on how many emails you're allowed to send through this command. Your PHP configuration files might also be locked down, or need some adjustments as well. So make sure you talk to your host if things aren't working smoothly. If you want more information about how to set up and debug your PHP, make sure you check out PHP Advanced Techniques or PHP and MySQL Essential Training. So, let's implement this into our form. We've been placing the PHP in the same file as our form. It let's us grab PHP variables and insert them into the form, but it's making our page a little long. So I'm going to move things into our process PHP file. So I'll grab this code right here and I'll cut it out of this page. And then I'll move it into the separate document.

You may have some text in there from before so I'm going to hit delete and paste our code right here. Now we need to call this page from within our index a php document. So I'm going to save this and I'll go back into my index.php and at the very top, I'm going to use php's include command to call process.php into index.php. This is like including a JavaScript js file into our page. The commands from that page get copied from the file and inserted in place into this file. And don't worry, the form will still work in the same way. So I'm going to save the index file and I'll switch back to the process.php document.

So one thing you don't want to do is mail anything unless our form is error-free. So I'm going to create a form error variable and set that to false. I'll place that right here. Now, if any of our validation or sanitizing rules generates an error, we'll set this variable to true. Next, we'll create an if statement that verifies that there are no errors and place our mail function inside that. So, the next step is to prepare the information you want to send. We'll need an email to send to, a subject, and a message. If you want to, you can also pass along some other information, like a from and reply to address.

I'm going to create a series of variables for this. When I'm doing this subject I'm going to use the my name variable here to print out the users name when I get an email from this page. I usually like to put a title for the page that this form is coming from, otherwise, I might get a bunch of emails without knowing which page they come from. The message can really be just about anything you want, including any of the variables from the form. For right now, we'll just put in a simple text message. Now, we'll also add some additional header information into the message. Now usually in here if you've asked for an email address, you can add that person's email address, usually in a variable called like email, to the message.

That way the person who receives emails from this form can just hit the reply button on their email program. And here I'll just identify that this is from my form processor. This is not a real email address. Now, some headers have to be separated by slash r and slash n's. In reply to, you may want to put an email so that if the person reading the mail from the form decides to reply to the user when the user receives that message, they're able to hit their reply and see the originator's email address.

Now we can issue the mail command. I'm doing an IF statement because the mail returns a boolean value. So if your server is successful in emailing the form, it's going to return true, otherwise, it'll return false. And then here, I'm just passing the variables over to the mail command. Now, in my form, I want to output a message that will say, thank you for filling out our form, if the form is successful. So I'll create this MSG variable. If the mail program is not successful, I'll send a different message. So as I mentioned before, PHP doesn't actually mail anything. Mailing is handled by your server's send mail command. So if there's any errors all you're going to get is this message saying that there was a problem.

If you want to look for actually errors you have to do it in your server's logs. Now we need to add something on the index.php page that outputs a message variable in the page if our form email is successful. So let me go back to my index.php document in here right after the body tag. I'm going to add a message. So if the message variable is set, then we're going to print out a message to the user. So I'm going to save this and I'll refresh the page. And I'll fill out my form really quick. Just a couple of, fields here, just the ones that I need. And I'm going to hit the send button.

And it says, thanks for filling out our form, so this message is printing out, which means that our email should have been successful. If I check my email application, you can see the message from the user. And if you open that up, you can see that the message has been sent and it has the user's name, plus the little message that I typed in the PHP file. So, let me go ahead and add the rest of the data to the email and usually you end up printing out all the variables just like you did here, but I want to do something different. This might make it easier for you to access the data in the future later on. I'm going to add the data to the form and usually you end up printing out variables along with some text, but I'm going to do something a little bit different. I'm going to create an associative array out of the form elements and then use the function to convert them to the JSON format.

This is going to make it easier for us to access the data if we want it in the future, and it's something we may be able to pass to JavaScript as an object if we want to. JSON is a really popular format that a lot of people use to store data especially in JavaScript. If you want to learn more about JSON, make sure you watch my course on JavaScript and JSON. We're going to put this right outside our mail section, which started right here, because it's something that we may want access to in other places. So essentially you need to create dissociative array out of all the existing variables.

Now I don't want to type all of that in, so I've created a closed image file. And I'm going to grab this associated array and switch back into my main program. And right here I'm going to paste this and this is just a regular PHP associated array. You create an array, and you create a key for each variable, and then you use this equal and greater than sign and then you put a name of a variable. You separate them with commas, this is a little bit like the JSON format. And then, all I have to do is change the message variable to encode our array as a JSON file. So, in message, I'm going to replace this with the PHP method JSON encode and then pass it along the variable of our array.

now I'm going to save that, I'll go ahead and reload the page. I'll type something in again, and I'll hit the send button. We get a thank you notice and let's go check our email. So, you can see I got the new email and if I click on this new one you'll see that I have a JSON object that I received as an email message. So you can see that emailing stuff with PHP is pretty easy. It's just a function that accepts some inputs. Do remember that you may have some problems depending on how your host is set up. So, if things aren't working just right, make sure you check with your host provider.

Show transcript

This video is part of

Image for Validating and Processing Forms with JavaScript and PHP
 
Expand all | Collapse all
  1. 3m 36s
    1. Welcome
      1m 9s
    2. What you should know
      1m 2s
    3. Using the exercise files
      1m 25s
  2. 12m 50s
    1. Understanding forms
      2m 2s
    2. Working with form fields
      7m 4s
    3. Using the form tag
      3m 44s
  3. 19m 23s
    1. Using input validation
      2m 10s
    2. Adding required fields and placeholders
      1m 31s
    3. Constraining numeric and date fields
      2m 32s
    4. Accepting multiple entries
      1m 41s
    5. Limiting uploads by MIME types
      2m 35s
    6. Assisting text input with a datalist
      1m 55s
    7. Constraining with regular expression patterns
      6m 59s
  4. 42m 13s
    1. Accessing forms
      3m 57s
    2. Looking up form elements
      3m 35s
    3. Handling focus changes
      2m 47s
    4. Detecting the onchange event
      4m 31s
    5. Using the selectedIndex property
      2m 30s
    6. Dynamic validation with regular expressions
      7m 0s
    7. Creating a generic input validation function
      4m 31s
    8. Validating in older browsers with Modernizr
      7m 32s
    9. Interrupting form submission with onsubmit
      5m 50s
  5. 15m 20s
    1. Understanding jQuery
      3m 47s
    2. Validating on submit with jQuery
      3m 45s
    3. Building interactive jQuery validation
      2m 34s
    4. Using the jQuery Validation plugin
      5m 14s
  6. 32m 57s
    1. Communicating with PHP servers
      2m 27s
    2. Retrieving data from superglobals
      8m 18s
    3. Using server-side validation
      4m 59s
    4. Adding in-page validation
      5m 22s
    5. Mirroring input data back to the user
      7m 46s
    6. Sanitizing form input
      4m 5s
  7. 43m 29s
    1. Mailing form data
      8m 28s
    2. Understanding file uploads
      3m 1s
    3. Uploading files
      9m 20s
    4. Processing form data with AJAX
      8m 14s
    5. Preparing your database
      5m 50s
    6. Pushing data
      8m 36s
  8. 1m 17s
    1. Next steps
      1m 17s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Validating and Processing Forms with JavaScript and PHP.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.