New Feature: Playlist Center! Pick a topic and let our playlists guide the way—like a learning mixtape.

Start learning with our library of video tutorials taught by experts. Get started

Validating and Processing Forms with JavaScript and PHP
Illustration by

Adding in-page validation


From:

Validating and Processing Forms with JavaScript and PHP

with Ray Villalobos

Video: Adding in-page validation

So far, we've been processing the fields in a separate PHP document. Another option is to make the form and the processor be on the same page. That makes it easier to provide on-page feedback. So, in this video, I'll show you how to move the form from our external script to the current page. I'm going to make some changes to the form first. To be a little bit more secure, I'll change the method of this page to Post. I'll change the Action field so that the form submits to itself. We could just use the name of this page as the action, but I'll use another PHP superglobal called Server to make the form submit to itself. So, right here I'm going to create a short PHP script. It'll just echo the superglobal server and it'll pass along as the parameter php underscore self.
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

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
Validating and Processing Forms with JavaScript and PHP
2h 51m Intermediate Apr 23, 2013

Viewers: in countries Watching now:

Validating web forms is a critical skill for any web developer, ensuring that the data that's submitted is complete, accurate, and nonmalicious before it's sent off to the server. Join author Ray Villalobos in this course as he shows how to validate input from site visitors with HTML5, JavaScript, and jQuery and then process the data with PHP. Plus, learn how to email form data and save it in a MySQL database so that it's ready for other applications.

Topics include:
  • Understanding forms
  • Adding required fields and placeholders
  • Accepting multiple entries
  • Limiting uploads
  • Handling focus changes
  • Validating with regular expressions
  • Working with older browsers
  • Building jQuery validation
  • Using server-side validation
  • Sanitizing form input
  • Uploading files
  • Sending form data to a database
Subjects:
Developer Web Web Design Databases Web Development
Software:
JavaScript PHP
Author:
Ray Villalobos

Adding in-page validation

So far, we've been processing the fields in a separate PHP document. Another option is to make the form and the processor be on the same page. That makes it easier to provide on-page feedback. So, in this video, I'll show you how to move the form from our external script to the current page. I'm going to make some changes to the form first. To be a little bit more secure, I'll change the method of this page to Post. I'll change the Action field so that the form submits to itself. We could just use the name of this page as the action, but I'll use another PHP superglobal called Server to make the form submit to itself. So, right here I'm going to create a short PHP script. It'll just echo the superglobal server and it'll pass along as the parameter php underscore self.

So, I'm going to save that and if I refresh my page. So, if you take a look at the source code for this page, you can see that the action is the same as the URL for this form. I'm also going to modify the button at the bottom of the screen so that it passes along a name and a value. We'll use that to test out the form has been submitted before we process it. So, I'm going to skip all the way to the bottom and find my submit button and add the name, action, and the value submit. So, save that. Now we can make sure that the form has been submitted with the Post method, and that it's received an action parameter.

So, I'll skip all the way back up to the top, and I'm going to add a little PHP script here as well. And here I'll make sure that the request method we get back from the server is set to Post. And also that the action field that we received from the form is not empty. And then, I'll end my IF statement. I'm going to save, and refresh my page to make sure we don't make a mistake. It's always a good idea to type a little bit of the script, and then just refresh your pages to make sure you don't make any mistakes. If you do, it will look something, let's go ahead and delete one of these parenthesis.

And if you save that and you try it, it's going to give you this kind of error, depending on how your server is set up for error reporting. So, let me save that, refresh. Now we're ready to move the script from our separate page into this page. So, I'm going to go to my process.php script and just grab most of the content from right here. I'm going to copy, I'm going to leave this page alone, and I'll close out, and then I'll paste this code right here. And I'll go ahead and indent it a little bit. So, I'm going to save and come back here and just try to submit the form without filling any fields. So, you see the errors are coming up just fine, they don't have any styles and they're not within the form.

That's easy enough to fix. It would be a little bit nicer if the errors were right underneath the fields that they belong to. To do that we're going to create a series of variables, and feed the text of the errors into those variables, and then print them underneath each input field. So, instead of just echoing everything, I want to create an error variable called err_MyName and set it equal to the error that we get from the form. Now I want to add the class right here and I could just add class equals and then in double quotes. But because I already have the double quotes right here, it may cause a problem.

So, I'm going to delete the outside double quotes here and replace them with single quotes. That way when I use double quotes inside, it won't be a problem. So, the class for this is going to be error and I'm going to to do that to all the other errors. So, I'm going to save this, and I'll refresh my page and none of my errors are printing out right now because they're just being stored in variables. So now I need to put those variables underneath each input field. So, I'm going to look for the Name input field, and right underneath that, I'm going to create a PHP script. And if the variable exists, I'll use the If Set method, of each one of those variables.

Then I'll echo the value of that variable to the screen. So, I'm going to save this, and just test it out by refreshing the page. And if you had filled out the form before, the error should come up just fine, right underneath the element. So, all we gotta do is take this and copy it a few times with the different variable names. So, the pattern match would also happen right here by the name, and the password feedback would happen, right here for the length. And I'll put the other one in the confirmation section.

So, I'm going to save this, and try to submit the form and I get all of my errors. Let's try to submit the form, filling some of the fields out. So now, I don't get the field that says that this is a required field, because I did fill it out. But it still gives me the second error that I didn't put it in the right format. So, let me put it in the right format, I'm going to hit Enter to submit. So, that's working pretty well, let's try the other one. So, the password, I'll make the password, password and I'll hit Send. It's telling me that it doesn't match, because I didn't fill out the other field.

So, by moving the form into the page, we were able to add some feedback from the script directly into the form. So, we have a small problem and that is that after the user submits the form. Any fields they have already filled out will disappear. We'll take care of that in the next movie.

There are currently no FAQs about Validating and Processing Forms with JavaScript and PHP.

 
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.
Upgrade now


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 Upgrade now

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

Notes cannot be added for locked videos.

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.