29

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • 1
    As some have noted below, the form's name shouldn't really be important. If you need to distinguish data from one form or another it's probably better for each form to have a different action. – rojoca May 10 '09 at 22:02

11 Answers11

94

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>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ayman Hourieh
  • 132,184
  • 23
  • 144
  • 116
18

You can do it like this:

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

Check the posted values

if (isset($_POST['myform'])) {
    $values = $_POST['myform'];

    // $login = $values['login'];
    // ...
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Seb
  • 189
  • 1
  • 2
  • I really like that way of going about it! My only question is, how would that work with input groups like checkboxes or radios? Is `name="myform[chkgrp][]"` valid? – eklingen Jan 11 '18 at 17:18
13

The form name is not submitted. You should just add a hidden field to each form and call it a day.

Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
  • Really? That seems like a half-hearted implementation of forms... o.O Still, I feel better for being unable to see how it works. – David Thomas May 10 '09 at 20:14
  • @ricebowl, A form's name is only useful for DOM manipulations and such. – strager May 10 '09 at 20:15
  • 2
    @ricebowl: understand, too, that this is how HTTP works. It has nothing to do with PHP. – Narcissus May 10 '09 at 23:02
  • @Strager, I hadn't realised that 'til now. I thought it would have more use, for some reason. @Narcissus, I was intending my criticism, unjust as it may be, to be read as against browsers/http, rather than php. – David Thomas May 11 '09 at 15:28
9

In the form submitting button (id method of form is post):

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

In the PHP file:

if (isset($_POST['commentData'])){
    // Code
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ruwantha
  • 2,603
  • 5
  • 30
  • 44
5

For some reason, the name of the submit button is not passed to the superglobal $_POST when submitted with Ajax/jQuery.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
albator
  • 859
  • 1
  • 10
  • 18
  • That may be true, but how does that answer the question? Can you elaborate? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/21206518/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Sep 26 '21 at 21:20
3

Use a unique value on the submit button for each form like so

File 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>

File email.php

<?php
    if (isset($_POST["submit"])) {
        switch ($_POST["submit"]) {
            case "contact":
                break;
            case "support":
                break;
            default:
                break;
        }
    }
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
xinthose
  • 3,213
  • 3
  • 40
  • 59
2

As petervandijck.com pointed out, this code may be susceptible to XSS attacks if you have it behind some kind of log-in system or have it embedded in other code.

To prevent an XSS attack, where you have written:

<?php echo "$weight"; ?>

You should write instead:

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

Which could even be better written as:

<?=htmlentities($weight); ?>
Community
  • 1
  • 1
Lachlan McDonald
  • 2,195
  • 2
  • 21
  • 25
  • 2
    ?> is not supported everywhere. I noticed some shared hosting, even when up-to-date has the option switch off in the php configuration file, even when php version is up-to-date – happy Mar 14 '13 at 15:10
  • This should be a comment to the question, not an answer. – Peter Mortensen Sep 26 '21 at 21:16
  • The link is broken. Are you referring to [PeterV's answer](https://stackoverflow.com/questions/846020/how-to-access-the-forms-name-variable-from-php/847819#847819)? – Peter Mortensen Sep 26 '21 at 21:18
2

You can use GET in the form's action parameter, which I use whenever I make a login/register combined page.

For example: action="loginregister.php?whichform=loginform"

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Elttob
  • 33
  • 2
  • 9
1

I had a similar problem which brought me to this question. I reviewed all the preceding answers, but ultimately I ending up figuring out my own solution:

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

It seamlessly and efficiently accomplishes the following:

  • Passes the form name attribute via a hidden input field, without using the fallible value attribute of the submit button.
  • Works with both GET and POST methods.
  • Requires no additional, independent JavaScript.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
James Anderson Jr.
  • 760
  • 1
  • 8
  • 26
  • 1
    Hi James thanks for your answer it was helpful to me. Could you please expand on why the submit button's value is fallable, thank you. – Frazer May 03 '20 at 18:03
0

You could just give a name to the submit button and do what needs to be done based on that. I have several forms on a page and do just that. Pass the button name and then if button name = button name do something.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adrian Fischer
  • 119
  • 3
  • 10
0

Only the names of the form fields are submitted, but the name of the form itself is not. But you can set a hidden field with the name in it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gumbo
  • 643,351
  • 109
  • 780
  • 844