【问题标题】:PHP sticky form code within form building echo statement表单构建回显语句中的 PHP 粘性表单代码
【发布时间】: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


【解决方案1】:

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

另外,对于 case 0 中的 false 选项,您在 PHP 中使用 &lt;?php。这样做的结果是它试图回显那部分 PHP,然后当它到达 False 周围的引号时它会翻转。

【讨论】:

  • 我只是想让这些字段保持粘性,这意味着当表单未完成时按下提交按钮时,用户会收到一条消息告诉他们完成它,但字段没有丢失用户已经填写的答案。其余的代码将被编写——这是我唯一想要弄清楚的——如何能够以这种方式回显表单,同时也回显粘性字段。如果表单都是 HTML 并且我没有尝试使用 PHP 构建它,这会很容易,但是由于 HTML 是由 PHP 代码构建的,所以很棘手。
  • 还不错。您的所有 HTML 输入都有名称,这些名称是提交表单时用于 POST 数组中的变量的键。您需要做的是将表单提交到验证 POST 数组中的元素的 PHP 脚本,如果任何输入无法验证,则将它们发送回原始表单页面并使用 POST 数组重新填充表单值.为了避免必须执行两个 POST 请求,您可以只将表单页面提交给自身,然后当且仅当设置了 $_POST 时在同一页面上运行验证检查。
【解决方案2】:

当 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 变量以在每个输入元素中回显。

【讨论】:

    【解决方案3】:

    我怀疑您对 - (1) 关于 PHP 和您的数据正在发生的事情有些困惑。 (2)如何让它做你想做的事。

    我查看了“questionTwo”并提供了一些代码,希望能帮助您“掌握”正在发生的事情。

    首先:这里有一些对“questonTwo”具有“粘性值”的代码,您可以运行和使用:

    Codepad - Viper-7 : Q26806928/php-sticky-form-

    解释:

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

    2) 当您生成 HTML 代码以输出“单选按钮”时,您知道其 unique 值,您需要检查 $_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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 2013-11-09
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多