php_mysql_apache [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

php_mysql_apache [Electronic resources] - نسخه متنی

Julie C. Meloni

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Accessing Form Input with User-Defined Arrays


The previous example showed how to gather information from HTML elements that submit a single value per element name. This leaves us with a problem when working with SELECT elements, when it possible for the user to choose one or more items. If we name the SELECT element with a plain name, like so



<select name="products" multiple>

the script that receives this data has access to only a single value corresponding to this name. We can change this behavior by renaming an element of this kind so that its name ends with an empty set of square brackets. We do this in Listing 9.3.

Listing 9.3

An HTML Form Including a SELECT

Element



1: <html>
2: <head>
3: <title>Listing 9.3 An HTML form including a SELECT element</title>
4: </head>
5: <body>
6: <form action="listing9.4.php" method="POST">
7: <p><strong>Name:</strong><br>
8: <input type="text" name="user">
9:
10: <p><strong>Address:</strong><br>
11: <textarea name="address" rows="5" cols="40"></textarea>
12:
13: <p><strong>Select Some Products:</strong> <br>
14: <select name="products[]" multiple>
15: <option value="Sonic Screwdriver">Sonic Screwdriver</option>
16: <option value="Tricoder">Tricorder</option>
17: <option value="ORAC AI">ORAC AI</option>
18: <option value="HAL 2000">HAL 2000</option>
19: </select>
20:
21: <p><input type="submit" value="send"></p>
22: </form>
23: </body>
24: </html>

Put these lines into a text file called listing9.3l, and place that file in your Web server document root. Next, in the script that processes the form input, we find that input from the "products[]" form element created on line 14 is available in an array called $_POST[products]. Because products[] is a SELECT element, we offer the user multiple choices using the option elements on lines 15 through 18. We demonstrate that the user's choices are made available in an array in Listing 9.4.

Listing 9.4 Reading Input from the Form in Listing 9.3


1: <html>
2: <head>
3: <title>Listing 9.4 Reading input from the form in Listing 9.3</title>
4: </head>
5: <body>
6: <?php
7: echo "<p>Welcome <b>$_POST[user]</b></p>";
8: echo "<p>Your address is:<br><b>$_POST[address]</b></p>";
9: echo "<p>Your product choices are:<br>";
10: if (!empty($_POST[products])) {
11: echo "<ul>";
12: foreach ($_POST[products] as $value) {
13: echo "<li>$value";
14: }
15: echo "</ul>";
16: }
17: ?>
18: </body>
19: </html>

Put these lines into a text file called listing9.4.php, and place that file in your Web server document root. Now access the form in Listing 9.3 with your Web browser and fill out the fields. Figure 9.2 shows an example.

Figure 9.2. Form created in Listing 9.3.


On line 7 of the script in Listing 9.4, we access the $_POST[user] variable, which is derived from the user form element. On line 10, we test for the $_POST[products] variable. If $_POST[products] is present, we loop through it on line 12, and output each choice to the browser on line 13. The text within the value attribute of the selected OPTION element becomes one of the stored values in the array.

Submit the form and you might see something like that shown in Figure 9.3.

Figure 9.3. Sample output of Listing 9.4.


Although the looping technique is particularly useful with the SELECT element, it can work with other types of form elements. For example, by giving a number of check boxes the same name, you can enable a user to choose many values within a single field name. As long as the name you choose ends with empty square brackets, PHP compiles the user input for this field into an array. We can replace the SELECT elements from lines 1518 in Listing 9.3 with a series of check boxes to achieve the same effect:



<input type="checkbox" name="products[]"
value="Sonic Screwdriver">Sonic Screwdriver<br>
<input type="checkbox" name="products[]" value="Tricorder">Tricorder<br>
<input type="checkbox" name="products[]" value="ORAC AI">ORAC AI<br>
<input type="checkbox" name="products[]" value="HAL 2000">HAL 2000<br>


/ 323