<?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" value="Submit">
</form>
</div>
</div>
</body>
</html>
要查询问题的html页面
<?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="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="'.$question_id.'" value="No">
<input type="hidden" name="option_b" value="'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
}
}
?>
基本上,此表单询问yes or no
是否使用单选按钮。 $option_a == 'Yes'
和$option_b == 'No'
。问题是这样的&#34;你发烧了吗?&#34;。因此,当我提交的价值没有传递给&#39; Answer.php&#39;页。
<?php
include 'conn.php';
if(isset($_POST['Submit']) && !empty($_POST['Submit'])){
echo $_POST['option_a'];
echo 'succeed';
}
else{
echo 'no data';
}
?>
在此页面中有错误undefined_index值,但仍然是echo成功。
答案 0 :(得分:0)
您的HTML代码应如下所示:
<input type="radio" name="whatevername" value="Yes">
<input type="hidden" name="whatevername" value="No">
您可以使用PHP插入所需的任何值,但您需要单选按钮名称相同。 然后,如果你想在PHP中回应它,你可以使用:
echo $_POST['whatevername']; //same name you used in the form
答案 1 :(得分:0)
您正在传递值作为单选按钮的名称
name="'.$option_a.'"
这将导致您名字=“是”
您正在尝试提取echo $_POST['option_a'];
,其中option_a
未定义。
试试这个
<input type="radio" name="'.$question_id.'" value="Yes">
<input type="hidden" name="option_a" value="'.$option_a.'">
其他单选按钮
相同答案 2 :(得分:0)
Try this one :
<?php
include 'conn.php';
function getQuestion($conn) {
$query = "SELECT * FROM question ";
$result = mysqli_query($conn, $query);
if($result){
echo '<div class="A4">';
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 '
<div class="A4">
<h2 class="qtitle">'.$question_body.'</h2>
<form action="Answer.php" method="post">
<label for='.$question_body.'>Yes</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_a.'">
<label for="'.$question_body.'">No</label>
<input type="radio" name="radioQuestions[]" value="'.$question_id.'-'.$option_b.'">
<input type="hidden" name="submitted" value="submitted"><hr>';
}
echo'
<input type="submit" name="Submit" value="Submit">
</form>
</div>';
}
}
?>
<?php
include 'conn.php';
if(isset($_POST['submitted']) && !empty($_POST['submitted'])){
$questionAndOptions = $_POST['radioQuestions'];
foreach ($questionAndOptions as $questionAndOption) {
$arrQuestionAndOption = explode("-", $questionAndOption);
echo $arrQuestionAndOption[0]; //question
echo $arrQuestionAndOption[1]; //option
}
echo 'succeed';
}
else{
echo 'no data';
}
?>