首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从PHP访问表单的“名称”变量

如何从PHP访问表单的“名称”变量
EN

Stack Overflow用户
提问于 2009-05-10 20:10:43
回答 11查看 108.9K关注 0票数 28

我在尝试建立一个BMI计算器。这应该允许人们使用公制或帝国式的测量。

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

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

代码语言:javascript
复制
<?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
    }
?>

我验证了它是否有效(虽然目前没有验证,但-I不想过多地干扰我的问题);我已经添加了表单,但还没有为帝国添加处理。

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2009-05-10 20:13:37

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

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

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

您可以按以下方式使用代码:

代码语言:javascript
复制
<form name="myform" method="post" action="" enctype="multipart/form-data">
    <input type="hidden" name="frmname" value=""/>
</form>
票数 92
EN

Stack Overflow用户

发布于 2013-02-19 13:13:04

你可以这样做:

代码语言:javascript
复制
<input type="text" name="myform[login]">
<input type="password" name="myform[password]">

检查已张贴的值

代码语言:javascript
复制
if (isset($_POST['myform'])) {
    $values = $_POST['myform'];

    // $login = $values['login'];
    // ...
}
票数 18
EN

Stack Overflow用户

发布于 2009-05-10 20:12:27

未提交表单名称。您应该在每个表单中添加一个隐藏字段,并将其称为一天。

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/846020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档