kangjacob 2023-02-19 20:02 采纳率: 96.8%
浏览 15
已结题

checkbox 为什么只能读取一个值的数组?

为什么checkbox总是只能获取数组的最后一个值?
下面是我的表格页面

<!doctype html>
<html>
<head>
<meta charset="utf-8">

<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
<title>配餐前的会员信息录入</title>
<style type="text/css">
    td{
        text-align: left;
        font-size:25px;
    }    
    table{
        border-spacing: 9px;
    }
    input[type="checkbox"]{
        width:25px;
        height:25px;
    }
    input[type="radio"]{
        width:25px;
        height:25px;
    }
    h1{
        font-size:28px;
    }
    h2{
        font-size:28px;
        color:red;
    }
    p{
        font-size:25px;
    }
    input[type="submit"]{
        width:90px;
        height:60px;
        font-size:26px;
    }
    input[type="reset"]{
        width:90px;
        height:60px;
        font-size:26px;
    }
    input[type="text"]{
        width:100px;
        height:30px;
        font-size:26px;
    }
</style>
</head>
<body>
    
    <form action='error_check.php'  method="post">
    
    
    
    <?php 
        session_start();//这里必须一开始就要启用,不然后面的session无法设置
        
        require_once("../dataBase.php");//connect the database
        
           $userName=$_SESSION['userName'];
        $sql="SELECT testTime FROM result_table WHERE userName='$userName'";
        $result=$conn->query($sql);
        $recordNumber=$result->num_rows;
        $testTime=array();//建立一个数组
        if($recordNumber>0){
            while($row=$result->fetch_assoc()){
                $testTime=$row;    //把数值循环放入数组中,感觉数组就是来者不拒,所有的赋值都会被纳入大家庭
            }
            
        }else{
            echo '您尚未进行评测,请从首页进入"中华医学会九种体质评测”';
        }
         $testTime=max($testTime);//求出数组中最大的数,从而获取最近的一次体质评测的次数,同时通过同样的命名,将数组取消掉
        //上面已经明确了用户最近的评测数据了,接下来就要读出其对应的体质评测结果了
        $sql="SELECT yangxu,yinxu,shire,tanshi,qixu,qiyu,xueyu,tebing,pinghe FROM result_table WHERE userName='$userName' and testTime='$testTime'";
        $result=$conn->query($sql);
        //下面不做记录是否为零的判断了,因为经过之前的筛选,一定有记录
        $body_feature=array();//设置一个数组用来装入结果进行排序,取出最前面的两个体质
        while($row=$result->fetch_assoc()){
            $body_feature=$row;
        }
        $max=max($body_feature);
        $bodyOne=array_search($max,$body_feature);//获得了最大值对应的键名
        //接着将数组降序排列,然后就可以根据数组的顺序读出第二个值,然后在通过该值找到对应的键名
        /*rsort($body_feature);//这里降序排列,第二大的序列自然就是 1
        $secMax=$body_feature[1];通过这种排序取键名的方式,虽然数组在,但是键名被改换成了数字,不能取到体质名称,所以用不了,只能使用后面的删除最大数,然后重新取数的办法获得第二大的值,然后通过该值而获得键名了
        $bodyTwo=array_search($secMax,$body_feature);
        echo $bodyTwo;
        */
        unset($body_feature[$bodyOne]);//这里根据键名删除了第一大的数值,便于后面使用函数找第二大的数值
        $secMax=max($body_feature);
        $bodyTwo=array_search($secMax,$body_feature);
        //echo $bodyTwo;
        $name_array=array("阳虚"=>"yangxu","阴虚"=>"yinxu","湿热"=>"shire","痰湿"=>"tanshi","气虚"=>"qixu","气郁"=>"qiyu","血瘀"=>"xueyu","特禀"=>"tebing","平和"=>"pinghe");//建立这个数组便于转换为对应的中文名字
        $bodyOne=array_search($bodyOne,$name_array);
        $bodyTwo=array_search($bodyTwo,$name_array);
        $_SESSION['bodyOne']=$bodyOne;
        $_SESSION['bodyTwo']=$bodyTwo;
    ?>
        <br>
        <h2><?php 
            if($_SESSION["form_caution"]!= Null){
            echo $_SESSION["form_caution"]; }
            ?>
        </h2>
    
    <h1>您最近的主要体质是<a href="../loginPage.php">(如未测评请点击)</a><br></h1>
    <p><?php echo $bodyOne ?>  <?php echo $bodyTwo ?></p>
    <hr>
    <h1>请勾选与您相关的疾病或状态(非必选)<br></h1>
    <table >
        <tr>
            <td>
            <input name="person_situation[]" type='checkbox' value='postnatal'>产后
            </td>
            <td>
            <input name="person_situation[]" type='checkbox' value='obesity'>肥胖
            </td>
        </tr>
        <tr>
            <td>
            <input name="person_situation[]" type='checkbox' value='pregnacy'>孕期
            </td>
            <td>
            <input name="person_situation[]" type='checkbox' value='thin'>消瘦
            </td>    
        </tr>
        <tr>
            <td>
            <input name="person_situation[]" type='checkbox' value='insomnia'>失眠
            </td>
            <td>
                <input name="person_situation[]" type='checkbox' value='diabetes'>糖尿病
            </td>
            </tr>
        <tr>
            <td>
            <input name="person_situation[]" type='checkbox' value='hypertension'>高血压
            </td>
            <td>
            <input name="person_situation" type='checkbox' value='coronary_artery_disease'>冠心病
            </td>
            </tr>
        <tr>
            <td>
            <input name="person_situation[]" type='checkbox' value='high_uric_acid'>高尿酸
            </td>
            <td>
            <input name="person_situation" type='checkbox' value='hyperlipidemia'>高血脂
            </td>
            </tr>
        <tr><td>
            <input name="person_situation[]" type='checkbox' value='asthma'>哮喘
            </td>
            <td>
            <input name="person_situation[]" type='checkbox' value='hyperthyroidism'>甲亢
            </td>
            </tr>
        <tr>
            <td>
            <input name="person_situation[]" type='checkbox' value='hypothyroidism'>甲减
            </td>
            <td>
            <input name="person_situation[]" type='checkbox' value='tumour'>肿瘤
            </td>
        </tr>
    </table>
    
<hr>
    <h1>请填写您的个人情况<br></h1>
    <p>
    <input name='person_sex' type="radio" value='male'>        
    <input name='person_sex' type='radio' value='female'><br><br>
    体重:<input name="person_weight" type="text" >KG(千克)<br><br>
    身高:<input name="person_height" type="text">CM(厘米)<br><br>
    年龄:<input name="person_age"    type="text"><br><br>
    <br>
    </p>
    <input type="submit" value="提交">
            
    <input type="reset" value="重填">
    </form>
    

</body>
</html>




点击提交的 errorCheck.php页面来进行判断 person_situation 这个数组 总是只能显示出最后的一项?为什么?下面是errorCheck.php的代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">

<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
<title>Untitled Document</title>
</head>
<?php  
    session_start();//启动session,这样后面才能调用
    $_SESSION["person_situation"]=$_POST["person_situation"];
    $person_situation=$_POST["person_situation"];
    $count_recorder=count($person_situation);
    echo $count_recorder;
    $_SESSION["person_age"]=$_POST["person_age"];
    $_SESSION["person_weight"]=$_POST["person_weight"];
    $_SESSION["person_height"]=$_POST["person_height"];
    $_SESSION["person_sex"]=$_POST["person_sex"];
    //下面对于每个填入的数据进行是否为空值的判断
    if($_SESSION["bodyOne"]|$_SESSION["bodyTwo"]==NULL){
        $_SESSION["form_caution"]="请先返回网站首页完成体质评估";
        header("location:./memberInformation.php");
        exit();
    }
    if($_SESSION["person_age"==Null]){
        $_SESSION["form_caution"]="请先填入您的实际年龄";
        header("location:./memberInformation.php");
        exit();
    }
    if($_SESSION["person_weight"]==Null){
        $_SESSION["form_caution"]="请先填入您的实际体重";
        header("location:./memberInformation.php");
        exit();
    }
    if($_SESSION["person_height"]==Null){
        $_SESSION["form_caution"]="请先填入您的实际身高";
        header("location:./memberInformation.php");
        exit();
    }
    if($_SESSION["person_sex"]==Null){
        $_SESSION["form_caution"]="请先选择您的真实性别";
        header("location:./memberInformation.php");
        exit();
    }
    //var_dump($_SESSION["bodyOne"]);
    //var_dump($_SESSION["bodyTwo"]);
    var_dump($_POST["person_situation"]);
    //var_dump($_SESSION["person_sex"]);
    //var_dump($_SESSION["person_weight"]);
    //var_dump($_SESSION["person_height"]);
    //var_dump($_SESSION["person_age"]);
    echo "准备跳转了";
    ?>
<body>
    
</body>
</html>

  • 写回答

3条回答 默认 最新

  • 社区专家-Monster-XH 2023-02-19 20:57
    关注

    checkbox只能获取数组的最后一个值,是因为多个checkbox的name属性相同,提交到后台后会被视为一个数组,而且数组会被覆盖成最后一个checkbox的值,如果想要获取多个checkbox的值,需要将它们的name属性设为一个数组形式,如"checkbox[]",这样提交到后台后就可以获取到多个值了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月28日
  • 已采纳回答 2月20日
  • 创建了问题 2月19日

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存