Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
125 views
in Technique[技术] by (71.8m points)

How to access the form's 'name' variable from PHP

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 follows:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...