PHP - 无法访问已检查的复选框值

时间:2016-04-12 03:16:59

标签: php html mysql if-statement checkbox

对于我的数据库项目,我正在做这个表单,用户可以选择他想要的信息,它将显示在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

1 个答案:

答案 0 :(得分:1)

你错过了这里的引文:

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