如何从 PHP 访问表单的“名称"变量 [英] How to access the form's 'name' variable from PHP

查看:15
本文介绍了如何从 PHP 访问表单的“名称"变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a BMI calculator. This should allow people to use either metric or imperial measurements.

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

I realise that I could use hidden tags to solve my problem, but this has bugged me before so I thought I'd ask: I can use $_POST['variableName'] to find the submitted variableName field-value; but...I don't know, or see, how to verify which form was used to submit the variables.

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

My code's below (though I'm not sure it's strictly relevant to the question):

<?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 verified that it worked (though without validation at the moment -I didn't want to crowd my question too much) with metric; I've added the form but not the processing for the imperial yet.

推荐答案

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

To identify the submitted form, you can use:

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

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

The name of the form is not sent to the server as part of the POST data.

您可以使用以下代码:

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

这篇关于如何从 PHP 访问表单的“名称"变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆