【发布时间】:2014-11-07 17:38:31
【问题描述】:
我的表单从文本文件构建测试,并使用如下 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 中我尝试完成此问题的两种方法。
但是,这些都不是解决方案。如何使我的输入字段在构建回显语句的表单中保持粘性?
谢谢。
【问题讨论】:
-
对不起,
make input fields sticky,我不确定你的意思。您是否在询问如何在回显语句中使用三元运算符在给定条件匹配的输入元素上设置属性checked? -
我知道如何制作粘性字段 - 这是因为 HTML 是使用 php echo 语句构建的,我无法通过简单地插入普通的 PHP 代码来使其具有粘性。
-
Ohgodwhy - 我误读了你想问我的问题 - 在学校里,我们将在 php 中提交时保留其值的表单称为“粘性表单”——它只是意味着任何字段已经填写(检查,书面或其他方式)如果未提交表单,则保留用户在回发时输入的任何值(因此,如果出现错误,例如未填写整个表单,则不会清除表单提交)。所以我想要完成的事情与此有关,但由于 html 是用 php echo 语句构建的,所以它很棘手。
标签: php forms echo builder sticky