【问题标题】:PHP - Can not access the checkbox value that has been checkedPHP - 无法访问已选中的复选框值
【发布时间】:2016-08-02 11:03:50
【问题描述】:

对于我的数据库项目,我正在使用此表单,用户可以在其中选择他想要的信息,并将其显示在 html 页面上。目前我只实现遇到问题的复选框。

问题是当我在我的操作页面“fe_page2.php”中检查复选框时(对于我当前的实现,我只对用户检查“位置”+“管道类型”+“金额”的情况感兴趣) ",检查复选框是否被选中的 if 条件永远不会达到,而是总是进入 else 条件。代码如下。

这是我的基本 html 页面:

    <html>  
<head>
    <title>Flow Element</title>
</head>
<body>
    <h1 style="margin-left: 450px">Retrieve Flow Element Information</h1>
    <hr>
    <form action="fe_page2.php" method="post">
    All Times:
    <input type="checkbox" name="alltimes" onchange="changeFields()"><br><br>
    Start Date:
    <input type="date" name="startdate" id="startdate">
    Start Time:
    <input type="time" name="starttime" id="starttime"><br><br>
    Same Time:
    <input type="checkbox" name="sametime" id = "sametime" onchange="sameFields()"><br><br> 
    End Date:
    <input type="date" name="enddate" id="enddate">
    End Time:
    <input type="time" name="endtime" id="endtime"><br><br>

    <h3> Select fields of output</h3>
    <hr>

    Pipe Key: <input type="text" name="pipepirmary" id="pipekey"><br>

    <input type="checkbox" name="pipelocation" id="pipeLocation" value="value1" >
    Location<br>

    <input type="checkbox" name="Pipe-Type" id="pipetype" value="value2">
    Pipe-Type<br>

    <input type="checkbox" name="amount" id="amount" value="value3">
    Amount<br><br>

    Flow Element:<input type="text" name="fepirmarykey" id="fekey"/><br>

    <input type="checkbox" name="flow" id="flo">
    Flow<br>

    <input type="checkbox" name="temperature" id="temp">
    Temperature<br>

    <input type="checkbox" name="pressure" id="pres">
    Pressure<br><br>

    <input type="submit">
</form>
    <script>
        function changeFields() {

            if(document.getElementById("startdate").disabled == false){
                document.getElementById("startdate").disabled = true;
                document.getElementById("starttime").disabled = true;
                document.getElementById("enddate").disabled = true;
                document.getElementById("endtime").disabled = true;
                document.getElementById("sametime").disabled = true;
                if(document.getElementById("sametime").checked = true){
                    document.getElementById("sametime").checked = false;
                    }
                document.getElementById("startdate").value = null;
                document.getElementById("starttime").value = null;
                document.getElementById("enddate").value = null;
                document.getElementById("endtime").value = null;
            }
            else{
                document.getElementById("startdate").disabled = false;
                document.getElementById("starttime").disabled = false;
                document.getElementById("enddate").disabled = false;
                document.getElementById("endtime").disabled = false;
                document.getElementById("sametime").disabled = false;
            }

        }
        function sameFields() {
            if(document.getElementById("enddate").disabled == false){
                document.getElementById("enddate").disabled = true;
                document.getElementById("endtime").disabled = true;
                document.getElementById("enddate").value = null;
                document.getElementById("endtime").value = null;
            }
            else{
                document.getElementById("enddate").disabled = false;
                document.getElementById("endtime").disabled = false;
            }
        }
    </script>

</body>
</html>

这是php页面fe_page2.php

<?php

 require('dbconf.inc');

 db_connect();

 print"<pre>";
 print_r($_POST);
 print"</pre>";

 $pipelocation = $_POST[pipelocation];
 $pipetype = $_POST[Pipe-Type];
 $pipeamount = $_POST[amount];

 if($pipelocation=='value1' && $pipetype == 'value2' && $pipeamount == 'value3'){

    $qrySel="SELECT `Pipe_ID`, `Location`, `Amount`, `PipeType` FROM pipe order by Pipe_ID desc";
    $resSel = mysql_query($qrySel);

    if($resSel){

        while($rowpipe = mysql_fetch_array($resSel, MYSQL_ASSOC)){
            echo "Pipe ID:{$rowpipe['Pipe_ID']} <br> ".
            "Location: {$rowpipe['Location']} <br> ".
            "Amount: {$rowpipe['Amount']} <br>".
            "PipeType: {$rowpipe['PipeType']} <br>".
            "--------------------------------<br>";
        }

        echo "Fetched Data Successfully!\n";    

    }
 }
 else{
    echo"never went in!";
 }

 db_close();



?>

我尝试过不同的方法,例如

if(isset($pipelocation) && isset($pipetype) && isset($pipeamount)){
     .....
}

并从 html 页面中删除值并使用以下代码:

 if($pipelocation == 'on' && $pipetype == 'on' && $pipeamount == 'on'){
               ...
    }

但还是没有运气... 任何帮助将不胜感激。

提供的代码纯粹是我的工作,但确实包含来自下面提供的参考的代码片段:

https://www.youtube.com/watch?v=LZtXYa9eGGw

【问题讨论】:

    标签: php html mysql if-statement checkbox


    【解决方案1】:

    你错过了这里的引文:

    $pipelocation = $_POST['pipelocation'];
    $pipetype = $_POST['Pipe-Type'];
    $pipeamount = $_POST['amount'];
    

    【讨论】:

    • 天哪,谢谢。我现在觉得好傻。再次感谢!
    • 有时会发生。没什么好担心的。
    猜你喜欢
    • 1970-01-01
    • 2019-03-14
    • 2018-01-15
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 2021-06-03
    相关资源
    最近更新 更多