Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Once you've created a data entry form using your own custom HTML and Cascading Style Sheets, and prepared your database table to receive inserts, you can then apply a server behavior to the form. The server behavior creates all of the PHP code you need to collect the data from the form and send it to the server. I am working in the file explorers/explorerinsert2.php. This version of the form already has all of the data validation I need. I will show the form in the browser and show you its behavior.
When I click the Submit button, I get validation messages on all of the relevant text fields and also on the State Select control. I'll go back to Dreamweaver and then open the Server Behaviors panel. If you don't see Server Behaviors on the right, as it is in my setup, you can go to the menu and choose Window > Server Behaviors. In the Server Behaviors panel, I'll click the Plus button and then choose Insert Record. In the Insert Record wizard, I am asked for the form that I'm going to collect data from.
There is only one form in this page, and it has a name of explorerForm; the database connection, and again there is only one explorecalifornia; and then the table into which I want to insert data. I'll choose the explorers table. Just as when using the Insert Record wizard, Dreamweaver detects the database structure and figures out dynamically what data types will be used in formatting data to add to the database. Most of these columns are simple text columns, but the DOB is a date column in the database table, and so it's going to be formatted as a date in my PHP code.
Also, just like the wizard, I am asked which page I want to browse to after the Insert operation is done. I will click the Browse button. I'll make sure I am in the explorers folder in my current site, and I'll choose explorerlist.php and click OK. In a previous video, I described one issue that happens with the generated PHP code from Dreamweaver. When you're working with date values, Dreamweaver isn't very good at formatting the date as needed for MySQL, and I showed you in that video how to write a little bit of custom PHP code that would collect the data correctly and format it for MySQL.
Now I am going to show you how to create that code in a reusable form. In the _includes folder, I've created a file named formfunction.php. The folder is _includes under the site root, and I have made a copy of two parts of the PHP code for my previous exercise. I have a require_once command that includes the Zend library's date class, and I have also included a copy of the GetSQLValueString function, and in the case for the date type, I've added the custom code that retrieves the data expecting the format that the users has to type in: month, day and year, and reformated it as required by MySQL.
Here is how I will use that file in the generated PHP code in my data entry form. I will return to the file explorerinsert2.php and go to Code View, and up at the top, after the require_once command for the connection, I'll add another PHP code block and another require_once command. And I'll include that file formfunctions.php, using the relative address ../_includes/formfunctions.php.
I'll close the name of the file, the require_once command, add the semicolon and close the PHP code block. Now in my newly generated code in this file, I have another version of the GetSQLValueString function. I don't need to remove that function definition because it's wrapped inside this conditional block, asking whether the function already exists. Because I've now included the version from the formfunctions file, this definition will be ignored, and the version that has the correct date formatting will be used at runtime.
I'm ready to test my form. I'll save my changes and run the page in an external browser. If you're prompted to copy files to the server, say yes. I'll type in some names. I'll type in a date of birth using the required date format. I'll use 6/23/2009. I'll add an e-mail address. I'll ignore the Address and City this time, because the form doesn't require them to be entered, and I have set up my database now so that they can be left blank, and I will choose a State.
Then I will click Submit, and I'll be taken back to my list page, and I'll show you that the date was entered correctly because of the revised and customized date formatting in my PHP code that was included from that separate file. So in this video, I have shown you how to use the server behavior for inserting a record and apply it to an existing data entry form. I've also shown you how to handle the special case of date formatting to allow the user to type in a date value in whatever format you choose.
You simply need to match the format of the date as expected when you create your Zend date object and match it to the validation rule in your form. Then you format the date correctly, as expected by MySQL. The result is a very simple data entry form that collects the data from the user, formats it correctly, and then sends it to your MySQL database.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 86774 Viewers
80 Video lessons · 136036 Viewers
59 Video lessons · 54761 Viewers
52 Video lessons · 68653 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.