如何使用单个html表单处理多个操作

时间:2009-08-01 08:00:15

标签: php

我希望我能够清楚地了解我需要的东西。我所拥有的是一个函数,其中包含我正在开发的电子邮件客户端应用程序的一些html。应用程序的这一部分使用共同的转发,回复和“回复所有”表单。由于它们基本相同,我认为我会走精益路线,只需使用一个功能来处理这个问题。我在上面提到的3个动作之间可以看到的唯一真正的区别是,在对所有人的回复中,在表单的CC部分中将有多个电子邮件地址。对于前锋,没有(cc)和(to)框应为空白。我需要代表所有这些功能,我对最好的方法是什么感到困惑。任何人都可以提供任何帮助吗?感谢。

我当然可以发布html是必要的,我只是想开始光明。

编辑: 我差点忘了,如果表单验证失败,用户提交表单时会有POST值。


function compose($type,$contents=null)
{
    echo'
        <tr>
          <td>
            <tr>
              <td valign="top">
                <form method="post" action="">
                  <table width="100%" cellpadding="0" cellspacing="0" border="0" id="reply">
                    <tr>
                      <td><h2>'.$type.'</h2></td>
                    </tr>
                    <tr>
                      <td width="1%" valign="top" nowrap><b>To:</b><br><input name="to" id="focus" title="Enter a single system user here" value="" type="text" size="64"></td>
                    </tr>
                    <tr>
                      <td nowrap><b>Cc:</b><br><input name="cc"" value="" type="text" size="64"></td>
                    </tr>
                    <tr>
                      <td nowrap><b>Subject:</b><br><input name="subject" title="Enter your subject here" value="" type="text" size="64" maxlength="30"></td>
                    </tr>
                    <tr>
                      <td valign="top"><b>Message:</b><br><textarea name="message" title="Enter your message here" rows="5" cols="50" wrap="virtual"></textarea></td>
                    </tr>
                    <tr>
                      <td>&nbsp;</td>
                    </tr>
                    <tr>
                      <td><input type="hidden" name="id" value=""><input type="submit" name="send" value="Send"></td>
                    </tr>
                  </table>
                </form>
              </td>
            </tr>
          </td>
        </tr>';
}

1 个答案:

答案 0 :(得分:0)

编辑:发布代码的示例修改(我没有添加所有不同的情况,甚至没有更改输出,只是显示了概念 - 所有这些都是检查一种回复和检查'到' 'POST值。)

function compose($type,$contents=null)
{
    $toValue = '';
    if(isset($_POST['to']))
    {
        // Might want to actually validate this to prevent XSS, but this is just a basic example
        $toValue = $_POST['to'];
    }

    echo'
        <tr>
          <td>
            <tr>
              <td valign="top">
                <form method="post" action="">
                  <table width="100%" cellpadding="0" cellspacing="0" border="0" id="reply">
                    <tr>
                      <td><h2>'.$type.'</h2></td>
                    </tr>';

    if($type == "Reply") {
        echo'
                    <tr>
                      <td width="1%" valign="top" nowrap><b>To:</b><br><input name="to" id="focus" title="Enter a single system user here" value="' . $toValue . '" type="text" size="64"></td>
                    </tr>
                    <tr>
                      <td nowrap><b>Cc:</b><br><input name="cc"" value="" type="text" size="64"></td>
                    </tr>';
    }

    echo'
                    <tr>
                      <td nowrap><b>Subject:</b><br><input name="subject" title="Enter your subject here" value="" type="text" size="64" maxlength="30"></td>
                    </tr>
                    <tr>
                      <td valign="top"><b>Message:</b><br><textarea name="message" title="Enter your message here" rows="5" cols="50" wrap="virtual"></textarea></td>
                    </tr>
                    <tr>
                      <td> </td>
                    </tr>
                    <tr>
                      <td><input type="hidden" name="id" value=""><input type="submit" name="send" value="Send"></td>
                    </tr>
                  </table>
                </form>
              </td>
            </tr>
          </td>
        </tr>';
}

(原始查询) 此函数是处理表单的结果,还是首先显示表单?你的问题有点不清楚你正在谈论的过程中的哪一点。