Php,html表单提交

时间:2014-04-02 02:54:57

标签: php html

为什么我的代码总是返回"请填写所有字段"?

<?php
if($_POST['submitted']=='') {
    echo '
        <form action="" method="POST">
            Total Trip Time: <input type="text" name="EndTime"><br>
            Miles Per Hour:  <input type="text" name="Distance"><br>
                             <input type="hidden" name="submitted" value="1">
                             <input type="submit" value="Submit">
        </form>
    ';
} else if ($_POST['EndTime'] == '') {
    echo '
        <H1>Please fill out all fields</H1>
        <form action="" method="POST">
            Total Trip Time: <input type="text" name="EndTime"><br>
            Miles Per Hour:  <input type="text" name="Distance"><br>
                             <input type="hidden" name="submitted" value="1">
                             <input type="submit" value="Submit">
        </form>
    ';
} else if ($_POST['Distance'] == '') {
    echo '
        <H1 style="color: red;">Please fill out all fields</H1>
        <form action="" method="POST">
            Total Trip Time: <input type="text" name="EndTime"><br>
            Miles Per Hour:  <input type="text" name="Distance"><br>
                             <input type="hidden" name="submitted" value="1">
                             <input type="submit" value="Submit">
        </form>
    ';

} else {
    echo "<table border='1'>";
    for ($StartTime = 1; $StartTime <= $_POST['EndTime']; $StartTime++) {
        echo"<tr><td>".$StartTime."</td><td>".$StartTime*$_POST['Distance']."</td></tr>";
    }
}
?> 

3 个答案:

答案 0 :(得分:1)

以下简化表单(对我来说 - 请参阅http://www.floris.us/SO/form.php):

<?php
if(isset($_POST['submitted']) && $_POST['EndTime']!='' && $_POST['Distance']!='') {
    echo "<table border='1'>";
        for ($StartTime = 1; $StartTime <= $_POST['EndTime']; $StartTime++) {
            echo"<tr><td>".$StartTime."</td><td>".$StartTime*$_POST['Distance']."</td></tr>";
        }
    }
    else {
        if(isset($_POST['submitted'])) {
            echo "You did not fill out all fields!";
        }
        echo '
            <form action="" method="POST">
                Total Trip Time: <input type="text" name="EndTime"><br>
                Miles Per Hour: <input type="text" name="Distance"><br>
                <input type="hidden" name="submitted" value="1">
                <input type="submit" value="Submit">
            </form>                 
            ';
    }
?> 

答案 1 :(得分:1)

尝试使用$_REQUEST而不是$_POST。如果发生任何错误,请在此处发布并告诉我错误是什么。

<?php
    if(isset($_REQUEST['submitted']) == "" || isset($_REQUEST['EndTime']=='') || isset($_REQUEST['Distance'] =='')) {
       echo '
                <form action="" method="POST">
                    Total Trip Time: <input type="text" name="EndTime"><br>
                    Miles Per Hour: <input type="text" name="Distance"><br>
                    <input type="hidden" name="submitted" value="1">
                    <input type="submit" value="Submit">
                </form>                 
                ';
        }
        else {
     echo "<table border='1'>";
            for ($StartTime = 1; $StartTime <= $_POST['EndTime']; $StartTime++) {
                echo"<tr><td>".$StartTime."</td><td>".$StartTime*$_POST['Distance']."</td></tr>";
            }

        }
    ?>

答案 2 :(得分:0)

试试这样。使用jquery或javascript验证

<?php
if(isset($_POST['submitted']) &&  isset($_POST['Distance']) && isset($_POST['EndTime']))
{
 echo "<table border='1'>";
        for ($StartTime = 1; $StartTime <= $_POST['EndTime']; $StartTime++) {
            echo"<tr><td>".$StartTime."</td><td>".$StartTime*$_POST['Distance']."</td></tr>";
        }
        echo '</table>';
}else{
    echo '
            <div id="err" style="color:red;"></div>
            <form action="" method="POST" onsubmit="return validate()">
                Total Trip Time: <input type="text" id="EndTime" name="EndTime"><br>
                Miles Per Hour: <input type="text" id="Distance" name="Distance"><br>
                                <input type="hidden" id="submitted" name="submitted" value="1">
                                <input type="submit" value="Submit">
            </form>                 
          ';

}
?>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
function validate()
{
    endtime = $("#EndTime").val();                   
    distance = $("#Distance").val();
    submitted = $("#submitted").val();
    if(endtime == "" || distance == "" || submitted == "")
    {
        $("#err").html("Please enter all fields");
        return false;
    } else {
        $("#err").html("");
    }
    return true;
}

</script>