如果它与php中的数组中的答案匹配,如何显示表单答案

时间:2013-07-31 17:36:52

标签: php arrays forms

我一直在努力学习php并练习,我根据姓氏制作了一系列Family Guy角色。然后我尝试在表单中提出问题,我希望代码检查数组以查看它是否与数组中的正确答案匹配。我还是PHP的新手,这真的只是一次学习经历。代码看起来像这样......

<?php
$families = array(
"Griffin" => array(
"Peter",
"Louis",
"Chris",
"Stewie",
"Meg"
),
"Quagmire" => array(
"Glen"
),
"Brown" => array(
"Cleveland",
"Loretta",
"Junior"
)
);

?>

<html>
<head>
</head>
<body>
Which of these Family Guy Characters is part of the Griffin family?
<form action = "familyguyquestions.php" method = 'post'>
A: <input type = "radio" name = "cleveland">Cleveland
B: <input type = "radio" name = "glenn">Glenn
C: <input type = "radio" name = "meg">Meg
D: <input type = "radio" name = "quagmire">Quagmire
<input type = "submit" name = "submitQuestion">
</form>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

您可以使用这样的in_array函数进行检查:

if (in_array($_POST['answer'], $families['Griffin'])) 
{
 // true 
}   else
{
// false
}

您还需要为单选按钮设置正确的名称:

A: <input type = "radio" name = "answer" value="Cleveland">Cleveland
B: <input type = "radio" name = "answer" value="Glenn">Glenn
C: <input type = "radio" name = "answer" value="Meg">Meg
D: <input type = "radio" name = "answer" value="Quagmire">Quagmire

答案 1 :(得分:0)

您的单选按钮结构不正确。它们都应该具有相同的name,例如name="guess",每个字符名称都应该在值中,例如value="cleveland"

然后这是一件简单的事情:

if (isset($families['Griffn'][$_POST['guess']]) {
   ... correct ...
} else {
   ... wrong ...
}

但请注意,PHP数组键是CASE-SENSITIVE。您必须像在数组中一样在表单中使用完全相同的名称:

<input type="radio" name="guess" value="Cleveland"> This is correct
<input type="radio" name="guess" value="cleveland"> Incorrect, lower case c on the name.

答案 2 :(得分:0)

有两种方法可以做到这一点:

  • 将表单发送到您的php,检查答案并呈现一个页面,显示答案是否正常
  • 向服务器发出AJAX请求并返回包含答案是否正确的JSON并更新DOM

至于如何发送数据,你应该在你的单选按钮上有一个相同的name属性和一个隐藏的输入来知道要查看的数组以及数组键(因为你有一个数组阵列)。

HTML:

<html>
<head>
</head>
<body>
Which of these Family Guy Characters is part of the Griffin family?
<form action = "familyguyquestions.php" method = 'post'>
<input type="hidden" value="families" name="what_array" />
<input type="hidden" value="Griffin" name="what_array_key" />
A: <input type = "radio" name="answer" value = "cleveland">Cleveland
B: <input type = "radio" name="answer" value = "glenn">Glenn
C: <input type = "radio" name="answer" value = "meg">Meg
D: <input type = "radio" name="answer" value = "quagmire">Quagmire
<input type = "submit" name = "submitQuestion">
</form>
</body>
</html>

PHP:

if (false == isset($_POST['what_array'])) {
    // No array here, return an error
}
if (false == isset($_POST['what_array_key'])) {
    // No key here, return an error
}
if (false == isset($_POST['answer'])) {
    // No answer, return error
}

$the_array = $_POST['what_array'];
$the_array_key = $_POST['what_array_key'];
$the_answer = $_POST['answer'];

$the_array = ($$the_array);
if (false == isset($the_array)) {
    // Another array to look into was set, return error
}

if (true == in_array($the_answer, $the_array[$the_array_key])) {
    // Here the answer is ok
} else {
    // Wrong answer
}

注意$$the_array上的双美元符号。这是你如何通过字符串获取变量。如果$the_array是“家庭”,$$the_array将是您要查看的实际数组。

example