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

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

问题描述

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



我意识到我可以使用隐藏标签来解决我的问题,但是之前这已经给我带来了麻烦,所以我想我'd问:我可以使用 $ _ POST ['variableName'] 来查找提交的variableName字段值;但是......我不知道或不知道如何验证哪些表单是用于提交变量的。



我的代码如下(尽管我不确定这与问题严格相关):

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

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

?>
< ul id =bmi>
< li>重量(公斤)为:< span><?php echo$ weight; ?>< /跨度>< /锂>

< li>身高(以米为单位)是:< span><?php echo$ height; ?>< /跨度>< /锂>

< li>身体质量指数(BMI)为:< span><?php echo$ bmi; ?>< /跨度>< /锂>

< / ul>
<?php
}

else {
?>

< div id =formSelector>

< ul>
< li>< a href =#metric>指标< / a>< / li>
< li>< a href =#imperial>帝国< / a>< / li>
< / ul>

< form name =metid =metricaction =<?php echo $ _SERVER ['PHP_SELF'];?> method =postenctype =form / multipart>

< fieldset>
< label for =weight> Weight(< abbr title =Kilograms> kg< / abbr>):< / label>
< input type =textname =weightid =weight/>


< fieldset>

< label for =weight> Weight(< abbr title =磅>磅< / abbr>):< / label>
< input type =textname =weightid =weight/>

< label for =height>高度(英寸):< / label>
< input type =textname =heightid =height/
< input type =hiddenname =bmiSubmittedid =bmiSubmittedvalue =1 />
< / fieldset>

< fieldset>
< input type =resetid =resetvalue =Clear/>
< input type =submitid =submitvalue =Submit/>
< / fieldset>
< / form>

<?php
}
?>

我验证了它的效果(虽然目前没有验证 - 我不想拥挤我的问题太多)与度量;我已经添加了表单,但没有为帝国处理。

解决方案

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


  • 隐藏的输入字段。

  • 提交按钮的名称或值。
  • >


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



您可以使用以下代码:

 < form name =myformmethod =postaction = ENCTYPE = 多部分/格式数据 > 
< input type =hiddenname =frmnamevalue =/>
< / form>


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

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:

  • A hidden input field.
  • The name or value of the submit button.

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

You can use code as followed

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

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

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