Preparing Data for Input / Converting Form Elements
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
In this lesson we're going to finish our discussion of preparing your data for input. In particular, we're going to focus on converting form elements. Remember in our previous lessons, we talked about the fact that in your FileMaker database, each field could have a specifically formatted control, such as radio buttons and checkboxes and drop-town menus. Then we discussed how we would take these elements in FileMaker and display them on the web, including converting the different data formats so that they work for the HTML elements. We talked about the fact that a checkbox control actually creates a return separated list in FileMaker. Because of this, we had to use the explode function in PHP so that we could create an array of the different items and then loop through them and display them on the web as being checked or not checked. When we talk about converting our data or preparing it for input, we're talking about doing the reverse. We're going to look at the HTML controls and figure out what we need to do to prepare them so that they can be inputted back into FileMaker. To do this, we've modified our asset detail page so it's ready for us to go ahead and write the code we need to convert our HTML elements. This form consists of a check to see if the user has submitted this form or not. And what we're looking for is we're using the array key exists to find out if the post array contains an element that has a key of save changes. Remember that our form has a button that's called save changes. So if the user clicks on that button and submits the form, then in the post array we will have an element with this key. We've been using the key exist and it's one of the best methods to use. You can use either array key exists, which is the technical name, or its alia, key exists. Right now we're just going to output the variables so that we can see what the data looks like when we submit it. Let's go ahead and open this up inside of our browser, going to Chapter 11, lesson 05, select the asset lists and click on an item. Here you can see that we have different values coming right from our FileMaker database. We have a field called wait and a field called type. I'm going to look at them in the form so that you can see their input names. Here we have the radio list, which the name contains wait, and then we have our checkbox list with the name contains type. Let's go ahead and submit this and see what our variables look like in the post array. Notice that I have an element with the key of type. That's going to be our type checkbox. But we only have one value, medium. What we're supposed to have is three different values. Also notice that we don't have any elements for wait because none of the wait elements were selected. We need to take both of these different scenarios into consideration. We need to be able to grab all the different checkboxes that were selected and we need to consider whether or not they didn't select anything. Because if I unselect all my checkboxes, just like my radio buttons, now there's no element for type. First let's talk about how to deal with the fact that the element is not passed using radio buttons or checkboxes if nothing was selected. What we're going to want to do is up inside of our code, which processes our array, we're simply going to want to check to see if there's a key that contains type or wait. So if array key exists, type inside the post array, then let's go ahead and create our type variable. We'll say it's going to be equal to the post element with the type key. If not, then we need to account for an empty variable or no item. Type's going to be an empty string. That way we have something to pass to the FileMaker database. Now what we need to do is duplicate this for the wait, input, and if we have a wait element, then we will pass that value to our wait variable. If not, then we'll set wait to an empty string. Now we have something to work with so that we can pass an empty string into FileMaker and erase the values. We could have added values to begin with and then they were unselected. So we want to make sure we pass something to FileMaker. Now, the other neat thing we need to deal with is multiple items. If you remember, when you select multiple items, only one item, and in particular the last one actually comes through. The reason for this is if you look down inside of our form, you'll notice that the checkbox input element has the name type. Every time you repeat this element, which is what we're doing, we're overriding that. So we have multiple elements with the same name. When you do this, the HTML form is just going to pass their last overwritten element named type. And that's the value that gets passed. To get all the elements, you would either have to rename each one with a unique value, such as appending a variable that you would define in increments when you iterate, or you can simply use an open or closing square brackets, which will allow HTML then to build it's own internal array and pass that on when the form is submitted. Let's go ahead and use the open and closing brackets and then resubmit this after we refresh our new form. When we save changes and we look at the source, you can see that the type element now has an array of items. What we need to do now is we need to convert this array of items into a return separated list because that's how they're worked with in FileMaker. When we converted it from a list to an array, we used the explode function. To do the opposite, we use the implode function. So we're going to say that our type is actually equal to implode and the post type array and the glue that's going to bond each element together is going to be a new line return. Now, before when we were checking a new line return, we would look for a special character, slash N. Depending upon your operating system, you may need to use a slash N or a slash R for return character. If you use both of them, that's the safest bet. That will work for Macintosh and Windows. When we put this in here and then we var dump our type, we can see what it's going to look like. Here you see we have a return separated list. This concludes our lesson in converting our form elements so they're ready for input into our FileMaker database.
Tutorial Information
| Course: | FileMaker 9 & PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 33786 |
| ISBN: | 1-933736-99-2 |
| Release Date: | 2007-08-22 |
| Duration: | 9.5 hrs / 107 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 