个性化阅读
专注于IT技术分析

如何读取PHP中是否选中了复选框?

使用isset()函数

isset()函数是PHP中的内置函数, 它检查是否设置了变量, 并且该变量不是NULL。此函数还检查声明的变量, 数组或数组键是否具有空值, 如果存在, 则isset()返回false, 在所有其他可能情况下返回true。可以使用isset()函数解决此问题。

语法如下:

bool isset( $var, mixed )

描述:此函数接受多个参数。该函数的第一个参数是$ var。此参数用于存储变量的值。

程序:

<? php 
  
if(isset($_GET['submit'])) {
     $var = $_GET['option1'];
     if(isset($var)) {
         echo "Option 1 submitted successfully";
     }
}
?>
<html lang = "en">
<head>
     <title>srcmini Example</title>
     <meta charset = "utf-8">
     <meta name = "viewport" content = "width=device-width, initial-scale=1">
     <link rel = "stylesheet" href =
     "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
     <style>
         .gfg {
             font-size:40px;
             font-weight:bold;
             color:green;
         }
         body {
             text-align:center;
         }
     </style>
</head>
<body>
     <div class = "container">
         <div class = "gfg">srcmini</div>
         <h2>Form control: checkbox</h2>
         <p>The form below contains one checkbox.</p>
         <form method = "get">
             <div class = "checkbox">
                 <label><input type = "checkbox" name = "option1" 
                         value = "Option 1">Option 1</label>
                 <label><button name = "submit" value = 'true' 
                     class = "btn btn-default">SUBMIT</button>
             </div>
         </form>
     </div>
</body>
</html>

输出如下:

如何读取PHP中是否选中了复选框?1

使用empty()函数

empty()函数是PHP中的内置函数, 用于检查变量是否为空。

语法如下:

bool empty( $var )

描述:此函数确定变量是否为空。

例子:

<? php
if(!empty($_GET['submit'])) {
     $var = $_GET['option1'];
     if(isset($var)){
         echo "Option 1 submitted successfully";
     }
}
?>
<!DOCTYPE html>
<html lang = "en">
<head>
     <title>srcmini Example</title>
     <meta charset = "utf-8">
     <meta name = "viewport" content = "width=device-width, initial-scale=1">
     <link rel = "stylesheet" href =
     "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
     <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
     </script>
     <script src = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
     </script>
     <style>
         .gfg {
             font-size:40px;
             font-weight:bold;
             color:green;
         }
         body {
             text-align:center;
         }
     </style>
</head>
<body>
     <div class = "container">
         <div class = "gfg">srcmini</div>
         <h2>Form control: checkbox</h2>
         <p>The form below contains one checkbox.</p>
         <form method = "get">
             <div class = "checkbox">
                 <label><input type = "checkbox" name = "option1" 
                             value = "Option 1">Option 1</label>
                 <label><button name = "submit" value = "true"
                 class = "btn btn-default">SUBMIT</button>
             </div>
         </form>
     </div>
</body>
</html>

输出如下:

如何读取PHP中是否选中了复选框?2

参考:

  • http://php.net/manual/en/function.isset.php
  • http://php.net/manual/en/function.empty.php

赞(0) 打赏
未经允许不得转载:srcmini » 如何读取PHP中是否选中了复选框?
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

微信扫一扫打赏