php表单不发送任何$\u post值

h79rfbju  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(339)

问题.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>

前置函数.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>

            ';  
            }

    }

}

?>

答案.php

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

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

      print_r($_POST);
}

}

?>

值复选框和隐藏输入没有在answer.php页面上发送任何值。它没有发出任何错误或警告选项a'=='是'值和'问题id'==s1,s2,s3等等。每个'question\u id'都有自己的问题,如果是,用户必须勾选。所以我想在另一页上发送这些值。我希望你们能帮我。

bttbmeg0

bttbmeg01#

我可以理解你实际上想要做什么,但不确定你实际上试图通过将选项保存为name和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];
    }
}
}
?>

希望这有意义,有帮助。:)
有关复选框的更多信息,请单击此处。

相关问题