Use HTML form with PHP (3)

Source: Internet
Author: User
Tags date arrays html form variables

Amazing vanishing check box



It is important to recognize that a check box is only submitted when it is actually selected. Otherwise, its disappearance will tell you the facts you need to know: The user does not click the checkbox. When you use a check box, you can use the Isset () function to explicitly check whether a value is set:


Listing 7. Check if a check box is submitted

...

$contact _value = $HTTP _get_vars[' contact '];

echo $contact _value;

if (Isset ($contact _value)) {

The checkbox was clicked

} else {

The checkbox wasn ' t clicked

}

...

Get all form values

the

check box field is just one example of a situation where you might not be sure about the expected form-value name. Typically, you will find it useful to have a routine that accesses all form values in a common way.

Fortunately, because $HTTP _get_vars and its peers are just hashes, you can manipulate them with some of the attributes of the array. For example, you can use the Array_keys () function to get a list of all potential value names:


Listing 8. Gets the list of form value names

$form _fields = Array_keys ($HTTP _get_vars);

for ($i = 0; $i < sizeof ($form _fields); $i + +) {

    $thisField = $form _fields[$i];

    $thisValue = $HTTP _get_vars[$thisField];

    echo $thisField. "=". $thisValue;

    echo <br/> "; 

}

...



In this case, you actually combine several techniques. First, you retrieve an array of form field names and name them $form _fields. $form _fields arrays are just a typical array, so you can use the sizeof () function to determine the number of potential keys and iterate through each item. For each item, retrieve the name of the field and use that name to get the actual value. The resulting Web page looks as follows:


ship = Midnight Runner

Tripdate = 12-15-2433

Exploration = yes

Crew = Array



Here are two important things. First, the Contact field has no return value at all, as expected. Second, the crew value (as you might know by the way: its name is crew instead of crew[]) is an array rather than a value. In order to actually retrieve all values, you need to use the Is_array () function to detect all arrays and process them accordingly:


Listing 9. Working with arrays

...

for ($i = 0; $i < sizeof ($form _fields); $i + +) {

$thisField = $form _fields[$i];

$thisValue = $HTTP _get_vars[$thisField];

if (Is_array ($thisValue)) {

for ($j = 0; $j < sizeof ($thisValue); $j + +) {

echo $thisField. "=". $thisValue [$j];

echo "<br/>";

}

} else {

echo $thisField. "=". $thisValue;

}

echo "<br/>";

}

...



The result is all the data that has actually been submitted:


ship = Midnight Runner

Tripdate = 12-15-2433

Exploration = yes

Crew = Snertal

Crew = Gosny



Last Note: Point

Now that you have a Form action page that adapts to any form value you submit, you need to take a moment to consider a situation that often surprises the PHP programmer.

In some cases, the designer chooses to use a graphical button instead of a Submit button, as shown in Figure 2, and the code is shown in Listing 10.


Listing 10. Add graphic buttons

...

<tr>

<TD valign= "Top" >crew species: </td>

<td>

<select name= "crew[]" multiple= "multiple" >

<option value= "Xebrax" >Xebrax</option>

<option value= "Snertal" >Snertal</option>

<option value= "Gosny" >Gosny</option>

</select>

</td>

</tr>

<tr>

<TD colspan= "2" align= "Center" >

<input type= "image" Src= "Button.gif" name= "Formbutton"/>

</td>

</tr>

</table>

...

-->

Figure 2. Graphic buttons on a form



Note that although there is only one image in the previous illustration, there are two graphic buttons (or expected results). As a developer, you can see where the user clicked by checking the X and Y coordinates returned together with the values. In fact, submitting the form as-is may create URLs and query strings that end with the following:


... snertal&crew%5b%5d=gosny&formbutton.x=37&formbutton.y=14



Note the. x and. Y that are attached to the button name. However, if you intend to submit the page and then view the results, you will see:


ship = Midnight Runner

Tripdate = 12-15-2433

Exploration = yes

Crew = Snertal

Crew = Gosny

formbutton_x = 37

formbutton_y = 14



Please note that the dot (.) has been converted to an underscore (_). This may seem a bit strange, but it is necessary because the variable name in PHP cannot have a number, so $formbutton. x is an illegal variable name. In fact, any dot number in the form name-not just for the image button-is converted to an underscore.

Conclusion



In this article, you see several ways to access information that users submit through an HTML or XHTML form. How you handle this information depends on the version of PHP that you are using and whether you can access the form variables as global variables. In any case, form values can be used as arrays, and you can iterate through all available values using the attributes of an array.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.