0

我一直在尝试学习 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>
4

3 回答 3

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
于 2013-07-31T17:41:06.297 回答
0

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

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

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

但请注意,PHP 数组键是区分大小写的。您必须在表单中具有与在数组中完全相同的名称:

<input type="radio" name="guess" value="Cleveland"> This is correct
<input type="radio" name="guess" value="cleveland"> Incorrect, lower case c on the name.
于 2013-07-31T17:41:27.497 回答
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则将是您要查看的实际数组。

例子

于 2013-07-31T17:53:49.880 回答