This section has some fresh content:
1. checkbox with multiple options. The names of all similar checkboxes are the same, so it is difficult to distinguish them with a single value. Therefore, the input value here is an array.
I have been wondering why I have to consider arrays when processing input values. Now I know.
So what should we do in this section?
1. You need to add a different style to the name of the checkbox []; as shown below (the place in the Analects is not standard and is not surrounded by tags, but it is only used for demonstration purposes, ):
1 <input class = "books" type = "checkbox" name = "Books []" value = "Lunyu"> Analects of Confucius
In this way, the input value is an array, that is, $ _ post may contain $ books = ('lunyu ', 'shijing', 'guwenguanzhi '); (The Book of Songs and Gu wenjing are the other two inputs with the same name)
2. First, the checkbox and Radio learned in the preceding two sections are the same. If you do not select any checkbox, click the submit button, $ _ post does not contain the amount of $ books. We cannot even get an empty array $ books, fortunately, we know this situation (that is, when the user does not select any option to click the submit button, reflected in the Code is ($ _ Post & isset ($ _ post ['books ']) = false )), in this case, we can think of $ _ post ['books '] = array (); ($ books = array () cannot be set (), because this code is placed before mail_process.php, $ books is defined in mail_process.php, so $ books is undefined at this time .)
1 if(!isset($_POST[‘books‘])){2 3 $_POST[‘books‘] = array();4 }
3. Then, we need to save the user's input, that is, if other user input is invalid, make sure that the correct option is entered when the user enters the input again. The code used is as follows:
1 <input class = "books" type = "checkbox" name = "Books []" value = "Lunyu" 2 <? PHP if ($ _ Post & in_array ('lunyu ', $ books) {echo 'checked';}?> 3> Analects of Confucius
4. Add a verification item to verify that the user input must be greater than a specified value. We only need to try to obtain the number of checkboxes selected by the user, and then compare it with the set quantity.
1 $minBooks = 2;2 if($_POST && count($_POST[‘books‘]) < $minBooks){3 $errors[] = ‘books‘;4 }
Count ($ _ post ['books ']) returns the number in the array $ books (but at this time there is require mail_process.php, so $ books cannot be used ), that is, the number of books selected.
In this way, if in_array ('books ', $ errors), we can determine that the number of books selected by the user is less than $ minbooks, and the error message will be displayed accordingly. If this is not the case, it means that the previous course is not very studious ~~
Later, because we have converted this value to the same value as the previous input, we don't have to worry about the subsequent logic ~~
Dealing with multiple-choice form fields (3)-handling checkbox groups