【问题标题】:Why are the nested forms processed with $_SERVER['PHP_SELF'] (same html file) not displaying/processing correctly?为什么使用 $_SERVER['PHP_SELF'] (相同的 html 文件)处理的嵌套表单不能正确显示/处理?
【发布时间】:2016-03-20 03:07:46
【问题描述】:

我正在尝试以渐进式顺序获取用户输入,从而导致该输入通过电子邮件发送。通过电子邮件发送是另一个我还没有解决的问题,所以并不担心。 我遇到困难的部分是一旦用户进入“发送电子邮件?” (是/否)单选按钮,该问题的输入未正确处理。 通过使用单独的 php 文件作为表单操作,我进一步了解了这一点,但仍然收到与 emailName、emailAddress 和 emailMsg 不存在相关的错误(“注意:未定义的索引...”)。 此外,我仍然需要能够进一步使用 $_POST[athletes] 数组,但我猜当时它超出了变量范围。 因此,为了将所有这些结合在一起,我真的要问几个问题:

1) 如何让所有表单在同一个文件中协同工作?

2) 程序何时真正通过“发送电子邮件?”当我使用单独的 php 文件作为表单操作时,为什么会出现未定义的索引错误?

3) 当我尝试在代码的更下方使用运动员[] 数组时,为什么会出现错误?我应该以某种方式将数组值传递给代码的那部分吗?

用户解决问题的具体步骤是:

  1. 选中 1 个或多个运动员复选框,然后单击“显示选择”按钮。

  2. 为“发送电子邮件?”选择“是”并点击“提交”按钮。

  3. 由于某种原因重新启动代码。

任何帮助将不胜感激。另外,这是我的第一篇文章,如果我根据网站礼仪错误地问了这个问题,非常抱歉。 我也为长代码片段道歉,但我不确定哪些部分可能导致此错误。

<b><h1><center>Athelete Selection Screen</center></h1></b>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        <p>
            <fieldset>
                <legend>Athletes Available: </legend>
                <input type="checkbox" id="student1"
                    name="athletes[]" value="Student1 Test">
                    <label for="student1">Student1 Test</label><br/>
                        <font color="grey">Football - Running back</font><br/>

                <p>
                <input type="checkbox" id="student2"
                    name="athletes[]" value="Student2 Test">
                    <label for="student1">Student2 Test</label><br/>
                        <font color="grey">Soccer - Left Forward</font><br/>
                </p>

                <p>
                <input type="checkbox" id="student3"
                    name="athletes[]" value="Student3 Test">
                    <label for="student1">Student3 Test</label><br/>
                        <font color="grey">Baseball - Pitcher/Left Outfield</font><br/>
                </p>                    

            </fieldset>
            <p>
                <?php echo("\t\t\t\t\t"); ?><button type="submit" name="submit" value="submit">Display Selection(s)</button>
            </p>
    </form>

    <fieldset>
        <legend>Athletes You Selected: </legend>

        <?php
            if (!empty($_POST['athletes']))
            {

                echo "<ul>";
                foreach($_POST['athletes'] as $value)
                {
                    echo "<li>$value</li>";
                }
                echo "</ul>";   
        ?>
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                    <p>
                        <fieldset>
                        <legend>Send Email? </legend>
                        <input type="radio" id="Yes"
                            name="radioSendMsg[]" value="Yes">
                            <label for="student1">Yes</label>

                        <p>
                        <input type="radio" id="No"
                            name="radioSendMsg[]" value="No">
                            <label for="student1">No</label><br/>
                        </p>
                        <button type="submit" name="submitRadio" value="submit">Submit</button>
                    </p>
                </form> 
        <?php
                if (!empty($_POST['radioSendMsg']))
                {
                    foreach($_POST['radioSendMsg'] as $radioMsg)
                    {   

                        if($radioMsg == "Yes")
                        {
                            echo "\tPlease enter information regarding the email to be sent: ";
                            ?>

                            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                                <p>
                                    <label for="emailName"> Name: </label><br/>
                                    <input type="text" size="25" id="emailName" name="emailName" />
                                </p>
                                <p>
                                    <label for="emailAddress">E-mail Address: </label></br>
                                    <input type="text" size="25" id="emailAddress" name="emailAddress" />
                                </p>
                                <p>
                                    <textarea id="emailMsg" name="emailMsg" cols="30" rows="5"></textarea>
                                </p>
                                <button type="submit" name="emailSubmit" value="send">Send Message</button>
                            </form>
                            <?php
                                $msg = "Name:     ".$_POST['emailName']."\n";
                                $msg.= "E-Mail:   ".$_POST['emailAddress']."\n";
                                $msg.= "Message:  ".$_POST['emailMsg']."\n";

                                $msg.= "<ul>";
                                foreach($_POST['athletes'] as $value)
                                {
                                    $msg.= "<li>$value</li>\n";
                                }
                                $msg.= "</ul>";

                                $emailRecipient = "sjzerbib@gmail.com";
                                $emailSubject = "Athlete Selection Submission";
                                $emailHeaders = "From: Sebastien\n"."Reply-To: ".$_POST['emailAddress'];

                                mail($emailRecipient,$emailSubject,$msg,$emailHeaders);

                                echo "Message sent: \n".$msg;
                        }
                        else
                        {
                            ?> <p /> <?php

                            echo "\n\nNo email will be sent for your last athlete selection.";
                             ?>
                            <br/>Please click <a href="http://localhost/CheckFormTest.html">here</a> 
                                to return to the Athlete selection screen.

                            <?php
                        }
                    }
                }
            }

【问题讨论】:

  • 仅供参考:PHP_SELF and XSS。当然,这不是页面上唯一存在 XSS 漏洞的代码。看起来你也有邮件头注入漏洞:Proper prevention of mail injection in PHP
  • 应该都只是一种形式而不是 3 种形式吗?
  • @AlexanderO'Mara 感谢您提供有关安全漏洞的信息 - 老实说,甚至没有想过这一点。
  • @Dagon 我不确定。通过分离为 3 个不同的表单,我让它只在用户进行选择时显示下一个表单。但我不知道这是否是正确的做法。

标签: php forms


【解决方案1】:

当您提交表单时,仅包含该表单中包含的控件。例外情况是成功的控件,其 form 属性设置为所提交表单的 id 值。

所以,假设你有类似的东西:

<form id="form-1" method="post">
    <input type="text" name="first-input" />
</form>

<input type="text" name="second-input" />

唯一要提交的值是first-input。如果给second-input添加form属性:

<input type="text" name="second-input" form="form-1" />

那么表单的提交将包含这两个值。不幸的是,form 属性不受完全支持 (IE and Edge have no support)。

当然,您的标记是无效的,所以这是一个有争议的问题。对于初学者,you cannot nest a form within a form。浏览器如何响应违反该规则的标记取决于它的供应商,但根据我的经验,这有点不可预测。您还使用了已弃用的标签(&lt;font&gt;&lt;center&gt; 不再有效)和错误地嵌套元素(&lt;h1&gt; 是块级元素,而 &lt;b&gt; 是内联元素)。

如果您每次都进行完整提交(因此页面被提交给自身,然后重新加载),那么只需使用某种条件来仅在前面的表单提交成功时呈现依赖控件:

<?php

    $canDoNextStep = !empty($_POST['input-1']);

?>

<form id="form-1" method="post">
    <input type="text" name="first-input" />

    <?php if(canDoNextStep): ?>

    <input type="text" name="second-input" />

    <?php endif; ?>
</form>

最后,当您的浏览器解析和显示您的 HTML 时,空格(大部分)会被忽略,因此您可能会丢失字符串中的 \t\n 值,除非您担心有人选择时您的标记看起来如何使用表单时查看源代码。

【讨论】:

  • 谢谢!这很有帮助。我将尝试以更直接的方式重新制定表格。此外,我最后一次广泛使用 html 是在十年前,所以我肯定会研究已弃用的标签。我不小心把 /t 和 /n 留在了里面,但我注意到它们不起作用。我猜你的解释回答了这个问题。
猜你喜欢
  • 1970-01-01
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 2019-05-21
  • 2015-05-17
  • 2012-12-15
  • 2021-04-06
  • 1970-01-01
相关资源
最近更新 更多