【问题标题】:How to access the form's 'name' variable from PHP如何从 PHP 访问表单的“名称”变量
【发布时间】:2010-10-25 03:58:30
【问题描述】:

我正在尝试创建一个 BMI 计算器。这应该允许人们使用公制或英制测量。

我意识到我可以使用隐藏标签来解决我的问题,但这之前一直困扰着我,所以我想我会问:我可以使用$_POST['variableName'] 来查找提交的变量名字段值;但是...我不知道或不知道如何验证使用了哪个表单来提交变量。

我的代码如下(虽然我不确定它是否与问题严格相关):

<?php
    $bmiSubmitted     = $_POST['bmiSubmitted'];

    if (isset($bmiSubmitted)) {
        $height        = $_POST['height'];
        $weight        = $_POST['weight'];
        $bmi        = floor($weight/($height*$height));

        ?>
            <ul id="bmi">
            <li>Weight (in kilograms) is: <span><?php echo "$weight"; ?></span></li>

            <li>Height (in metres) is: <span><?php echo "$height"; ?></span></li>

            <li>Body mass index (BMI) is: <span><?php echo "$bmi"; ?></span></li>

            </ul>
        <?php
    }

    else {
    ?>

    <div id="formSelector">

    <ul>
        <li><a href="#metric">Metric</a></li>
        <li><a href="#imperial">Imperial</a></li>
    </ul>

        <form name="met" id="metric" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">

            <fieldset>
                <label for="weight">Weight (<abbr title="Kilograms">kg</abbr>):</label>
                    <input type="text" name="weight" id="weight" />

                <label for="height">Height (<abbr title="metres">m</abbr>):</label>
                    <input type="text" name="height" id="height" />

                <input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
            </fieldset>

            <fieldset>
                <input type="reset" id="reset" value="Clear" />

                <input type="submit" id="submit" value="Submit" />
            </fieldset>
        </form>

        <form name="imp" id="imperial" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="form/multipart">

            <fieldset>

            <label for="weight">Weight (<abbr title="Pounds">lbs</abbr>):</label>
                <input type="text" name="weight" id="weight" />

            <label for="height">Height (Inches):</label>
                <input type="text" name="height" id="height" /
            <input type="hidden" name="bmiSubmitted" id="bmiSubmitted" value="1" />
            </fieldset>

            <fieldset>
                <input type="reset" id="reset" value="Clear" />
                <input type="submit" id="submit" value="Submit" />
            </fieldset>
        </form>

    <?php
    }
?>

我用度量标准验证了它是否有效(尽管目前没有验证 - 我不想让我的问题过于拥挤);我已经添加了表格,但还没有处理英制。

【问题讨论】:

  • 正如下面一些人所指出的,表单的名称并不重要。如果您需要将数据与一种或另一种形式区分开来,最好让每种形式都有不同的操作。

标签: php forms


【解决方案1】:

要识别提交的表单,您可以使用:

  • 隐藏的输入字段。
  • 提交按钮的名称或值。

表单名称不会作为POST 数据的一部分发送到服务器。

您可以使用如下代码:

<form name="myform" method="post" action="" enctype="multipart/form-data">
    <input type="hidden" name="frmname" value=""/>
</form>

【讨论】:

    【解决方案2】:

    你可以这样做:

    <input type="text" name="myform[login]">
    <input type="password" name="myform[password]">
    

    检查发布的值

    if (isset($_POST['myform'])) {
        $values = $_POST['myform'];
    
        // $login = $values['login'];
        // ...
    }
    

    【讨论】:

    • 我真的很喜欢这种方式!我唯一的问题是,这将如何与复选框或收音机等输入组一起使用? name="myform[chkgrp][]" 有效吗?
    【解决方案3】:

    表单名称未提交。您应该只在每个表单中添加一个隐藏字段并收工。

    【讨论】:

    • 真的吗?这似乎是对表单的半心半意的实现...... o.O 不过,看不到它是如何工作的,我感觉好多了。
    • @ricebowl,表单的名称只对 DOM 操作等有用。
    • @ricebowl:也明白,这就是 HTTP 的工作方式。它与PHP无关。
    • @Strager,直到现在我才意识到这一点。出于某种原因,我认为它会有更多用处。 @Narcissus,我打算将我的批评视为针对浏览器/http,而不是 php。
    【解决方案4】:

    在表单提交按钮中(表单的id方法为post):

    <input type="submit" value="save" name="commentData">
    

    在 PHP 文件中:

    if (isset($_POST['commentData'])){
        // Code
    }
    

    【讨论】:

      【解决方案5】:

      由于某种原因,使用 Ajax/jQuery 提交时,提交按钮的名称没有传递给超全局 $_POST

      【讨论】:

      • 这可能是真的,但这如何回答这个问题?你能详细说明吗?请通过editing (changing) your answer 回复,而不是在 cmets 中(without "Edit:"、"Update:" 或类似的 - 答案应该看起来像是今天写的)。
      【解决方案6】:

      像这样在每个表单的提交按钮上使用唯一值

      文件index.html

      <form method="post" action="bat/email.php">
          <input type="text" name="firstName" placeholder="First name" required>
          <input type="text" name="lastName" placeholder="Last name" required>
          <button name="submit" type="submit" value="contact">Send Message</button>
      </form>
      
      <form method="post" action="bat/email.php">
          <input type="text" name="firstName" placeholder="First name" required>
          <input type="text" name="lastName" placeholder="Last name" required>
          <button name="submit" type="submit" value="support">Send Message</button>
      </form>
      

      文件email.php

      <?php
          if (isset($_POST["submit"])) {
              switch ($_POST["submit"]) {
                  case "contact":
                      break;
                  case "support":
                      break;
                  default:
                      break;
              }
          }
      ?>
      

      【讨论】:

        【解决方案7】:

        正如petervandijck.com 所指出的,如果您将其置于某种登录系统后面或将其嵌入其他代码中,则该代码可能容易受到 XSS 攻击。

        为了防止 XSS 攻击,您已经写了:

        <?php echo "$weight"; ?>
        

        你应该改写:

        <?php echo htmlentities($weight); ?>
        

        最好写成这样:

        <?=htmlentities($weight); ?>
        

        【讨论】:

        • ?> 并非所有地方都支持。我注意到一些共享主机,即使最新的选项在 php 配置文件中关闭,即使 php 版本是最新的
        • 这应该是对问题的评论,而不是答案。
        • 链接已损坏。你指的是PeterV's answer吗?
        【解决方案8】:

        您可以在表单的操作参数中使用 GET,每当我创建登录/注册组合页面时都会使用它。

        例如:action="loginregister.php?whichform=loginform"

        【讨论】:

        • Re "GET":习惯上不是用"get"(小写)(不是反问句)吗?
        【解决方案9】:

        我遇到了类似的问题,这让我想到了这个问题。我查看了所有前面的答案,但最终我找到了自己的解决方案:

        <form name="ctc_form" id="ctc_form" action='' method='get'>
        
            <input type="hidden" name="form_nm" id="form_nm">
        
            <button type="submit" name="submit" id="submit" onclick="document.getElementById('form_nm').value=this.closest('form').name;">Submit</button>
        
        </form>
        

        它无缝且高效地完成了以下任务:

        • 通过隐藏输入字段传递表单name属性,而不使用submit的错误value属性> 按钮。
        • 适用于 GETPOST 方法。
        • 不需要额外的独立 JavaScript。

        【讨论】:

        • 嗨,詹姆斯,感谢您的回答,这对我很有帮助。您能否详细说明为什么提交按钮的值会出错,谢谢。
        【解决方案10】:

        您可以只为提交按钮命名,然后根据该名称执行需要完成的操作。我在一个页面上有几个表格,我就是这样做的。传递按钮名称,然后如果按钮名称 = 按钮名称,则执行某些操作。

        【讨论】:

          【解决方案11】:

          仅提交表单字段的名称,但不提交表单本身的名称。但是您可以设置一个隐藏字段,其中包含名称。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-12-17
            • 1970-01-01
            • 2011-09-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多