我的表单从文本文件构建测试,并使用echo语句构建答案选择,如下所示:
function buildTest()
{
/*if((file_exists("files/questions.txt")) && ((file_exists("files/answers.txt")) && (filesize("files/questions.txt")!=0)) && (filesize("files/answers.txt") !=0))
{*/
$questionArray = array();
$questionArray = file("files/questions.txt"); //populate the questions array from a file
$answerArray = array();
$answerArray = file("files/answers.txt"); //populate the answers array from a file
//}
for ($i = 0; $i < 10; $i++)
{
echo "<div class=\"question\"><br />";
echo "<h3>Question #" . ($i + 1) . "</h3><br />";
echo $questionArray[$i] . "<br /><br />";
switch ($i)
{
case 0:
echo "<input type='radio' name='questionOne' value='True'" . if (isset($questionOne) && $questionOne=="True") echo "checked" . "?>/> True<br />";
echo "<input type='radio' name='questionOne' value='False' <?php if (isset($questionOne) && $questionOne=="False") echo "checked";?>/> True<br />";
break;
case 1:
echo "<input type='radio' name='questionTwo' value='false' /> Blood, Water, Fire, Wind and Earth<br />";
echo "<input type='radio' name='questionTwo' value='false' /> Diamond, Ice, Water, Fire and Wind<br />";
echo "<input type='radio' name='questionTwo' value='Metal, Water, Wood, Fire and Earth' /> Metal, Water, Wood, Fire and Earth<br />";
break;
case 2:
echo "Answer: <input type='text' name='questionThree' />";
break;
case 3:
echo "<input type='radio' name='questionFour' value='True' /> True<br />";
echo "<input type='radio' name='questionFour' value='False' /> False<br />";
break;
case 4:
echo "<input type='radio' name='questionFive' value='Goat' /> Goat<br />";
echo "<input type='radio' name='questionFive' value='false' /> Rat<br />";
echo "<input type='radio' name='questionFive' value='false' /> Dragon<br />";
break;
case 5:
echo "Answer: <input type='text' name='questionSix' />";
break;
case 6:
echo "<input type='radio' name='questionSeven' value='True' /> True<br />";
echo "<input type='radio' name='questionSeven' value='False' /> False<br />";
break;
case 7:
echo "<input type='radio' name='questionEight' value='false' /> Horse<br />";
echo "<input type='radio' name='questionEight' value='false' /> Fox<br />";
echo "<input type='radio' name='questionEight' value='Pig' /> Pig<br />";
break;
case 8:
echo "Answer: <input type='text' name='questionNine' />";
break;
case 9:
echo "<input type='radio' name='questionTen' value='True' /> True<br />";
echo "<input type='radio' name='questionTen' value='False' /> False<br />";
break;
}
echo "</div>";
echo "<br /><hr />";
}
以上显示了案例0我尝试完成此问题的两种方法。
然而,这些都没有提出解决方案。如何在构建echo语句的表单中使输入字段变粘?
谢谢。
答案 0 :(得分:0)
为什么不使用if
块来检查questionOne
是真还是假,然后采取相应的行动?
此外,对于案例0中的false
选项,您在PHP中使用<?php
。这样做的结果是,它试图回应那一点PHP,然后当它到达False
左右的引号时就会翻转。
答案 1 :(得分:0)
当php处理表单时,选中的单选按钮和选中的复选框会导致该元素的$ _POST变量的值为&#39; on&#39;。因此,如果用户没有完成表单以满足脚本的满意度,并且您希望选中复选框和单选按钮,则需要查看$ _POST元素通过值&#39;上&#39;在这种情况下,您需要包括已检查=&#39;已检查&#39;在html输入中。
例如,使用:
if($ _ POST [&#39; elementA&#39;] ==&#39; on&#39;)$ stickifierA =&#34; checked =&#39; checked&#39;&#34 ;;
将导致html输入:
<input type='checkbox' name='elementA' <?php echo $stickifierA;?>>
要检查他们是否在最后一次(失败)尝试填写表格时进行了检查。
您可以使用foreach循环检查所有内容并创建适当的php变量以在每个输入元素中回显。
答案 2 :(得分:0)
我怀疑你有一些困惑 - (1)关于PHP和你的数据发生了什么。 (2)如何让它做你想做的事。
我查看了'questionTwo',并提供了一些代码,希望能帮助您“了解正在发生的事情”。
首先:这里有一些代码具有'questonTwo'的'粘滞值',你可以运行和玩:
Codepad - Viper-7 : Q26806928/php-sticky-form-
说明:
1)组中的每个单独的单选按钮必须具有唯一值。它是'$ _POST'数组中返回的'value'。如果两个单选按钮具有相同的值,则您无法知道选择了哪一个。
2)当您生成HTML代码以输出“单选按钮”(您知道唯一值)时,您需要签入$ _POST以查看是否具有匹配值。如果是,则输出'checked',浏览器将其标记为已选中。
这就是我提供的所有代码:
如果'var_dumps'$ _POST提供,那么你可以看到代码正常工作。
哦,不要忘记在网络浏览器中“查看页面来源”以查看它实际收到的代码。
玩代码。希望它会变得更加清晰。
<?php // https://stackoverflow.com/questions/26806928/php-sticky-form-code-within-form-building-echo-statement
// This is the code for 'QuestionTwo'
// Run it and inspect the source code to see what is happening.
/*
* Please don't let someone look at the 'page source' and see the answers.
*
* <input item1 --- false
* <input item2 --- false
* <input item3 --- value
*
* is a clue as to the correct answer.
*/
/*
* To re-select the 'user selected' input item then the 'input items' must have unique values
* so that you can output 'checked' on the correct item when displaying it.
*/
// example:
$questions = array();
$questions['questionTwo'] = array('Q2_A1' => 'Blood, Water, Fire, Wind and Earth',
'Q2_A2' => 'Diamond, Ice, Water, Fire and Wind',
'Q2_A3' => 'Metal, Water, Wood, Fire and Earth',
);
$answers = array();
$answers['questionTwo'] = 'Q2_A2'; // for example
/*
* If there was some selected input then it will appear in the $_POST array...
* It MUST have a unique *value* so that we can identify which item the
* user selected.
*/
// display the input so you can see what is happening
/* debug */ if (!empty($_POST)) {
/* debug */ var_dump('Post array is:', $_POST);
/* debug */ }
// use PHP as a templating language...
?>
<!DOCTYPE html>
<html>
<head>
<title>sticky-form-code</title>
</head>
<body>
<form action="" method='POST'>
<div>
<?php foreach($questions['questionTwo'] as $answerCode => $answerText): ?>
<input type='radio' name='questionTwo' value="<?= $answerCode ?>"
<?php if (!empty($_POST['questionTwo'])): // user selected answer to this question? ?>
<?php if ($_POST['questionTwo'] === $answerCode): // check if matches this entry? ?>
checked
<?php endif; ?>
<?php endif; ?>
><?= $answerText ?><br />
<?php endforeach; ?>
</div>
<!-- check the answer if provided -->
<?php if (!empty($_POST['questionTwo'])): // is there a selected value in $_POST? ?>
<div>
<?php if ($_POST['questionTwo'] === $answers['questionTwo']): // match? ?>
correct!
<?php else: ?>
sorry...
<?php endif ?>
</div>
<?php endif; ?>
</div>
<div>
<input type='submit' value='Go For it!'>
</div>
</form>
</body>
</html>