How Do You Use PHP Variables in HTML?

Problem scenario
You have a .php file with PHP and HTML code.  You want an HTML text field to be display a pre-populated text value by default.  The user can then modify this value from the web UI (i.e., front-end).  You want this text value to be based on a PHP variable (calculated in earlier in the code).  How do you get your HTML code to display the value of a PHP variable?

Solution
Prerequisites
For an Ubuntu or Debian distribution of Linux consider these bullets to install Apache web server and PHP:

  • To install a web server (so the .php pages can be published and thereby be visible in a web browser) on an Ubuntu or a Debian distribution of Linux, run this:  sudo apt-get -y install apache2-bin libapache2-mod-php
    • This link may also be helpful.  You may also want to see this link if you are new to Apache web server on Ubuntu.
  • To install PHP on an Ubuntu or a Debian distribution of Linux, run this: sudo apt-get -y install php

For a RedHat distribution of Linux (e.g., CentOS/Fedora), consider these bullets to install Apache web server and PHP:


Procedures

This example illustrates how you can inject (or transfer) a PHP value into HTML code.  A text field will have the value of a PHP variable if you use this code a .php file and view it in a web browser.

<?php
$var1="continualintegration";
?>

<html>
<body>

<form action="anotherPage.php" method="post">

Check this text field out: <input type="text" name="pk" value=<?php echo $var1; ?>>

<input type="submit"2>
</form>

</body>
</html>

Leave a comment

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