PHP表单不发送任何$ _POST值

时间:2018-04-12 15:42:01

标签: php mysql

Question.php

    <?php
include 'Pre-function.php'

?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="CSS/Start.css">
</head>
<body>

<div class="nav">
    <a href="Homepage.php">Home</a>
    <a href="#news">News</a>
    <a href="#contact">Contact</a>
</div> 

<div class="question"> 
    <div class="A4">
        <form action="Answer.php" method="POST">
        <?php getQuestion($conn);  ?>
        <input type="submit" name="Submit">

        </form>
    </div>
</div>
</body>
</html>

预function.php

<?php
include 'conn.php';

function getQuestion($conn) {

    $query = "SELECT * FROM question ";
    $result = mysqli_query($conn, $query);

    if($result){

        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
           $question_id = $row['question_id'];
           $question_body = $row['question_body'];
           $option_a = $row['option_a'];
           $option_b = $row['option_b'];

            echo '

                <h2 class="qtitle">'.$question_body.'</h2>
                <label 
                for='.$question_body.'>Yes</label>
                <input type="checkbox" name="'.$option_a.'" value="'.$option_a.'">
                <input type="hidden" name="'.$question_id.'" value="'.$question_id.'">
                <hr>

            ';  
            }

    }

}

?>

Answer.php

<?php
include 'conn.php';
if(isset($_POST['Submit'])){

  if(isset($_POST['answer'])){

      print_r($_POST);
}

}


?>

值复选框和隐藏输入未在Answer.php页面上发送任何值。它没有发送任何错误或警告。的&#39; Option_a&#39; ==&#39;是&#39; 值和&#39; question_id&#39; == S1,S2,S3等。每个&#39; question_id&#39; 都有自己的问题,如果,用户必须勾选。所以我想在另一页上发送这些值。我希望你们中的任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)


我可以通过将选项保存为name和question_id作为隐藏输入来了解您实际想要做什么但不确定您实际想要实现的目标。

我如何处理这个问题是将question_id保存为选项的名称,如果是或否问题,我建议使用径向按钮。无论如何要解决您的问题,请在pre-function.php中对您的问题格式进行以下更改

<?php
include 'conn.php';

function getQuestion($conn) {

    $query = "SELECT * FROM question ";
    $result = mysqli_query($conn, $query);

    if($result){

        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
           $question_id = $row['question_id'];
           $question_body = $row['question_body'];
           $option_a = $row['option_a'];
           $option_b = $row['option_b'];

            echo '

                <h2 class="qtitle">'.$question_body.'</h2>
                <label>'.$question_body.'</label>
                <input type="checkbox" name="'.$question_id.'" value="'.$option_a.'">
                <input type="checkbox" name="'.$question_id.'" value="'.$option_b.'">
                <hr>

            ';  
            }
    }
}

?>

现在,每个具有两个选项的问题将具有与问题相对应的相同名称和值作为选项。现在你的Answer.php看起来像是

<?php
include 'conn.php';
if(isset($_POST['Submit'])){    // if submit is true

 // if you want to print all answers then loop through the question id
 // $_POST[$question_id] gives you the value of the answer 
 //i.e the option the user has chosen for that particular question

$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);

if($result){

    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
       $question_id = $row['question_id'];
       echo $_POST[$question_id];
    }
}
}
?>

希望这是有道理和有帮助的。 :)

检查here以获取有关复选框的更多信息。