表单构建echo语句中的PHP粘性表单代码


PHP sticky form code within form building echo statement

我的表单从文本文件构建测试,并使用以下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中我尝试过的两种方法来解决这个问题。

然而,这两者都没有提供解决方案。如何使表单构建回显语句中的输入字段具有粘性?

谢谢。

为什么不使用if块来检查questionOne是真是假,然后采取相应的行动?

此外,对于案例0中的false选项,您在PHP中使用<?php。这样做的结果是,它试图呼应PHP的那一点,然后当它到达False周围的引号时,它就翻了出来。

php处理表单时,选中单选按钮和复选框会导致该元素的$_POST变量的值为'on'。因此,如果用户没有按照脚本的要求完成表单,并且您希望选中复选框和单选按钮,则需要查看值为"on"的$_POST元素是通过哪些操作完成的,在这种情况下,您需要在html输入中包含checked="checked"。

例如,使用:

if($_POST['elementA']=='on')$stickifierA="checked='checked'";

将导致html输入:

<input type='checkbox' name='elementA' <?php echo $stickifierA;?>>

如果他们在最后一次(失败)尝试填写表格时检查了它,则进行检查。

您可以使用foreach循环来检查所有内容,并创建适当的php变量以在每个输入元素中进行回显。

我怀疑您对-(1)PHP和您的数据有一些困惑。(2) 如何让它做你想做的事。

我看过"问题二",并提供了一些代码,希望能帮助您"掌握"正在发生的事情。

首先:这里有一些"questonTwo"具有"粘性值"的代码,您可以运行和玩:

Codepad-Viper-7:Q26806928/php粘性表单-

说明:

1) 组中的每个单选按钮都必须具有唯一值。它是在"$_POST"数组中返回的"value"。如果两个单选按钮具有相同的,则无法知道选择了哪一个。

2) 当您生成HTML代码以输出"单选按钮"时,您知道其唯一值,您需要签入$_POST以查看是否有匹配的值。如果是,则输出"已选中",浏览器会将其标记为已选中。

这就是我提供的所有代码:

如果提供了'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>