Inserting values in PHP with HTML

In this PHP tutorial, you are going to learn about how to insert the values in PHP variable with the help of HTML form.

Insert Values in PHP

To insert values into PHP code, we generally take the help of our HTML code.

As we cant take values directly from the user in PHP as it is server side language we generally take the help of some HTML code.

We can write PHP code in the middle of HTML code but can’t write HTML code in the middle of PHP.

To do so we have to follow some steps:

  1. First, we have to create a form in html.
  2. Then write your statement and use html input tag to take some text and give it a name.
  3. Now create a submit button using submit tag in PHP.
  4. Close the form and body and close HTML.
  5. Now open PHP code using <?php.
  6. Create a variable to store the value which is thrown from HTML.
  7. Now import value by using predefined $_POST[“name_of_variable”] tag.
  8. Then print it using echo statement.
  9. Then exit PHP code using ?>

Example Program : Insert Values to PHP variable from HTML form through submit button

<html>

<body>

<form method="POST">

enter your text :<input type="text" name="input_value"><br>

<input type="submit" value="submit">

</form>

</body>

</html>

<?php

$a=$_POST["input_value"];

echo "--->";

echo $a ;

?>

Output:

enter your text:5
      [submit]

---> 5

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *