应用 isset() 函数

这是一个内置函数,能够检查是否设置了变量。

使用 isset() 函数,我们还可以检查数组、声明的变量或者数组键是否为空。
如果存在,isset() 函数返回 false,在所有可能的情况下返回 true。

isset() 函数的语法如下所示:

bool isset( $var, mixed )

现在,让我们看看检查复选框是否被选中的代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>onitroad 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>
    <?php
      if(isset($_GET['submit'])) { 
        $var = $_GET['option1']; 
	    if(isset($var)) { 
	      echo "Option 1 submitted successfully"; 
        } 
      } 
    ?>
    <div class="container">
      <div class="gfg">onitroad</div>
      <h2>Form control: checkbox</h2>
      <p>The form below contains one checkbox.</p>
      <form method="get" action="/form/submit">
        <div class="checkbox">
          <label>
            <input type="checkbox" name="option1" value="Option 1">Option 1
          </label>
          <button name="submit" value="true">SUBMIT</button>
        </div>
      </form>
    </div>
  </body>
</html>
如何在 PHP 中读取复选框是否被选中

在本教程中,我们可以找到有关如何读取 PHP 中是否选中复选框的综合信息。

其中我们将演示两个方便的函数,它们将了解 PHP 中是否选中了复选框。
这些函数是isset() 和empty()。

使用 empty() 函数

在本节中,我们将说明另一个内置函数:empty() 函数。

使用empty(),我们可以测试变量是否为空。

语法如下:

bool empty( $var )

如果选中复选框,让我们看看如何使用此函数进行读取:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>onitroad</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>
    <?php
      if(!empty($_GET['submit'])) { 
      $var = $_GET['option1']; 
      if(isset($var)){ 
        echo "Option 1 submitted successfully"; 
      } 
     } 
    ?>
    <div class="container">
      <div class="gfg">onitroad</div>
      <h2>Form control: checkbox</h2>
      <p>The form below contains one checkbox.</p>
      <form method="get" action="/form/submit">
        <div class="checkbox">
          <label>
            <input type="checkbox" name="option1" value="Option 1">Option 1
          </label>
          <button name="submit" value="true">SUBMIT</button>
        </div>
      </form>
    </div>
  </body>
</html>
日期:2020-06-02 22:15:51 来源:oir作者:oir