PHP gets form form radio button and check box values

伊谢尔伦
Release: 2023-03-07 15:04:01
Original
12520 people have browsed it

Get the value of the radio button

In the

form, radio buttons generally appear in groups. , with the same name value and different value values, in a group of radio buttons, only one may be selected at the same time.

Let's take an example of a radio button radio. In this code example, there are two radio buttons with name = "sexy". Select one of them and click the "Submit" button. Returns the value of the selected radio button.

The specific development steps are as follows:

(1) Use any development tool to create a PHP dynamic page and name it index.php.

(2) Add a form, add a set of radio buttons and a submit button, the code example is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>form</title>
</head>
<body>
<form action="" method="post" name="form1">
   <table width="500" border="0" cellpadding="0"  cellspacing="0">
      <tr>
         <td width="500" height="30">
            <input type="radio" name="sexy" value="男" checked>男
           <input type="radio" name="sexy" value="女">女
           <input type="submit" name="submit" value="提交">
         </td>
      </tr>
   </table>
</form>
</body>
</html>
Copy after login

Description: checked Attribute is used Setting the form element to be selected by default means that when the form page is initialized, the form element with the checked attribute is selected.

(3) Add PHP tag symbol anywhere outside the form element, then apply $_POST[] global variable to get the value of the radio button group, and finally pass The echo statement is output, and the code is displayed as follows:

<?php
 echo "您选择的性别为:";                   //输出字符串
 echo $_POST["sexy"];                      //输出被选中的单选按钮的值
?>
Copy after login

(4) Enter the running address in the browser, press the Enter key, and get the running result as shown below:

PHP gets form form radio button and check box values

Get the value of the check box

The check box enables multiple selections of items. When the viewer fills out the form, he or she needs to select multiple items. For example, if you need to select multiple songs at the same time while listening to music online, check boxes will be used. Check boxes generally have multiple options at the same time. In order to facilitate passing values, the name can be in the form of an array, the format is:

<input type = "checkbox" name="checkbox[]" value="checkbox1">
Copy after login

and then return The page can use the count() function to calculate the size of the array, and combine it with the for loop statement to output the value of the selected check box.

The following uses an example to explain how to obtain the value of the check box. In this example, a set of information is provided for the user to select, in which the name value is the array variable of mrbook[]. The information selected by the user is displayed on the processing page. If the array is empty, "You have no choice" is returned. The specific operation steps are as follows:

(1) Create a new index.php page and create A form form, add a set of check boxes and a submit button, the code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>form</title>
</head>
<body>
<form action="index.php" method="post" name="form1">
   <table width="500"  cellpadding="0"  cellspacing="0">
      <tr>
         <td width="500" height="40" align="center" valign="top">喜欢的图书类型:
                 <input type="checkbox" name="mrbook[]" value="艺术类" >艺术类
                 <input type="checkbox" name="mrbook[]" value="体育类" >体育类
                 <input type="checkbox" name="mrbook[]" value="理工类" >理工类
                 <input type="checkbox" name="mrbook[]" value="其他类" >其他类
                 <input type="submit" name="submit" value="提交">
         </td>
      </tr>
   </table>
</form>
</body>
</html>
Copy after login

(2) Add a PHP tag anywhere outside the form element, and then use $_POST [] global variable to get the value of the check button, and finally output it through the echo statement. The code is shown as follows:

<?php
if($_POST["mrbook"]!= null){                       //判断复选框如果不为空,则执行下面的操作
   echo "您选择的结果是:";                         //输出字符串
   for($i = 0; $i < count($_POST["mrbook"]);$i++){  //通过 for 循环语句输出选中复选框的值
      echo $_POST["mrbook"][$i]." ";                //循环输出用户选择的图书类别
   }
}
?>
Copy after login

(3) Enter the running address in the browser and press the Enter key to get the following picture Running results shown:

PHP gets form form radio button and check box values

The above is the detailed content of PHP gets form form radio button and check box values. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template