Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

forms

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.

like image 586
David Thomas Avatar asked May 10 '09 20:05

David Thomas


People also ask

How do you access form variables in php?

Within your PHP script, you can access each form field as a PHP variable whose name relates to the name of the form field. You can recognize variable names in PHP because they all start with a dollar sign ($). (Forgetting the dollar sign is a common programming error.)

How can I get HTML name in php?

Try this. $name = 'name'; $var = $_POST[$name]; The result will display the value of the name variable.

How can get submit button value in php?

Add type property with submit type="submit" to button tag this will submit the form then receive the form inputs value in php script with super global variable $_POST['input name'] or $_GET[] wdepends on from action property value by default GET.


2 Answers

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> 
like image 78
Ayman Hourieh Avatar answered Oct 07 '22 01:10

Ayman Hourieh


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'];     // ... } 
like image 42
Seb Avatar answered Oct 06 '22 23:10

Seb