如何获取php中选中复选框的值?

How to get value of checked checkbox in php?(如何获取php中选中复选框的值?)
本文介绍了如何获取php中选中复选框的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

代码如下:

<?php // operator Page
include ("classes/dbhelper.php");
include ("conf/conf.php");
$conf = new Dbconf();
$dbURL = $conf->get_databaseURL();
$dbUName = $conf->get_databaseUName();
$dbPword = $conf->get_databasePWord();
$dbName = $conf->get_databaseName();
$nameOfDbWithCustomers = $conf->get_tableName('customer');

if(isset($_REQUEST[session_name()]))
{
   session_start();
}
else
{
   header("Location: authorize.php");
}
if($_SESSION['usr_id'] == md5(crypt($_SESSION['login'],$_SESSION['pass'])))
{   
    echo "<script type='text/javascript' src='/jquery-1.6.js'></script>
        <form method='post'> 
        Name: <input type='text' name='Name' size='10' value=''>
        Post: <input type='text' name='Post' size='10' value=''>
        Section: <input type='text' name='Section' size='10' value=''>
        Company: <input type='text' name='Company' size='10' value=''>
        Phone Number: <input type='text' name='Phone_Number' size='10 value=''>
        e-mail: <input type='text' name='e-mail' size='10' value=''>
        Active: <input type='checkbox' name='Active' value=''> 
        <input type='submit' name='Search' size='10' value='Search'> <br>
        <input type='reset' name='Reset' value='Reset'>
        </form>

        <form method ='post'>
        SMS: <input type='checkbox' name = 'sms'>
        E-mail: <input type='checkbox' name = 'email' id='mailcheck'><br>
        <TEXTAREA NAME='message' WRAP='virtual' COLS='40' ROWS='3'>
        </TEXTAREA><br>
        <input type ='submit' name ='Send' size = '10' value = 'Send'>
        </form>

        <form action='upload.php'' method='post' enctype='multipart/form-data'>
        <input type='file' name='filename'><br> 
        <input type='submit value='Загрузить'><br>
        </form>";

    if (isset($POST['Send'])){

    }

    if (isset($_POST['Search'])){
        //*********************query*************************
        $name = isset($_POST['Name']) ? $_POST['Name'] : 0;
        $post = isset($_POST['Post']) ? $_POST['Post'] : 0;
        $section = isset($_POST['Section']) ? $_POST['Section'] : 0;
        $company = isset($_POST['Company']) ? $_POST['Company'] : 0;
        $phoneNumber = isset($_POST['Phone_Number']) ? $_POST['Phone_Number'] : 0;
        $eMail = isset($_POST['e-mail']) ? $_POST['e-mail'] : 0;
        $active = isset($_POST['Active']) ? "1" : "0";

        $array = array(
                 "name" => $name,
                 "post" => $post,
                 "section" => $section,
                 "company" => $company,
                 "phone_number" => $phoneNumber,
                 "email" => $eMail,
                 "status" => $active
               );

        $sql = "SELECT * FROM $nameOfDbWithCustomers";
        $sql_where = array();

        foreach($array as $key => $value)
            {
                if(!empty($value))
                $sql_where[] = $key." = "."'$value'";
            }
        if(count($sql_where) > 0)
            {
                $sql .=" WHERE ";
            }
        $sql.=" ".implode(" AND ", $sql_where);
        //*********************END query*************************
        $dbHelp = new DbHelper($dbURL, $dbUName, $dbPword, $dbName, '');
        $queryResult = $dbHelp->getDataFromDbByQuery($sql); 

        $table = "<table border=1 width=100% align=center>
";
        $table .= "<tr>
";
        $i = 1;
        while ($i < mysql_num_fields($queryResult)) {
            $meta = mysql_fetch_field($queryResult, $i);
            $i++;
            $table .= "<td>".$meta->name."</td>
";
        }
        $table .= "<td> Выбрать все: <input type='checkbox' name='cbname3[]' value='main' id='chkSelectAll'</td>
";
        $table .= "</tr>
";
        $i = 1;
        while ($row = mysql_fetch_assoc($queryResult)){     
            $table .= "<tr>
";
            $table .= "<td>".$row['name']."</td>
";
            $table .= "<td>".$row['post']."</td>
";    
            $table .= "<td>".$row['section']."</td>
";  
            $table .= "<td>".$row['company']."</td>
";  
            $table .= "<td>".$row['phone_number']."</td>
";  
            $table .= "<td>".$row['email']."</td>
";  
            $table .= "<td>".$row['status']."</td>
";  
            $table .= "<td>".$row['lock_time']."</td>
";  
            $table .= "<td>".$row['reason_for_blocking']."</td>
";  
            $table .= "<td><input type='checkbox' class=".check."  name='cbname3[]' id='chkItems' value=".$row['id']." /></td>";
            $table .= "</tr>
";
            $i++;
        }
        $table .= "</table>
";
        echo $table;
    }
}

?>
<script type="text/javascript">
    $(document).ready(function() {
        $("#<%=chkSelectAll.ClientID %>").click(function() {
            $("#<%= chkItems.ClientID %> input:checkbox").attr('checked',this.checked);
        });

        $("#<%=chkItems.ClientID %> input:checkbox").click(function(){
          if($("#<%= chkSelectAll.ClientID %>").attr('checked') == true && this.checked == false)
             $("#<%= chkSelectAll.ClientID %>").attr('checked',false);

             if(this.checked == true)
                CheckSelectAll();
        });

      function checkSelectAll()
      {
          var flag = true;
           $("#<%=chkItems.ClientID %> input:checkbox").each(function() {
                if(this.checked == false)
                    flag = false;
            });
              $("#<%= chkSelectAll.ClientID %>").attr('checked',flag);
      }  
    });

当用户按下发送"按钮时,我想从表格中获取选中复选框的值.我该怎么做?

I want to get value of checked checkbox from table when user press on "send" button. How can I do that?

推荐答案

你应该为 HTML 代码中的复选框赋值

You should assign a value for checkbox in HTML code

Active: <input type='checkbox' name='Active' value='1'> 

然后如果复选框被选中,它包含在 POST 中分配的值,否则您必须在未选中复选框时进行设置

and then if the checkbox is checked it contains assigned value in POST otherwise you have to set when checkbox is unchecked

$active = isset($_POST['Active']) && $_POST['Active']  ? "1" : "0";

这篇关于如何获取php中选中复选框的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Subtract time in PHP(在 PHP 中减去时间)
Limit execution time of an function or command PHP(限制函数或命令 PHP 的执行时间)
How to sum N number of time (HH:MM Format)?(如何求和 N 次(HH:MM 格式)?)
How to get current time in milliseconds in PHP?(如何在 PHP 中以毫秒为单位获取当前时间?)
How to check if time is between two times in PHP(如何检查时间是否在 PHP 中的两次之间)
Convert number of minutes into hours amp; minutes using PHP(将分钟数转换为小时数分钟使用 PHP)