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
- 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:
- How do you get Apache Web Server to publish a specific .PHP file on a RHEL server?
- How Do You Get Apache Web Service to Start Automatically after a Reboot of a CentOS Linux Server?
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>