How do i add php to an existing html

I havent used php before, i figured i would learn. I am trying to figure out how to add php blocks to my existing html.

That’s great. Don’t hesitate to ask if you encounter any problems

1 Like

Assuming you have PHP installed, you would do this…

<label for="oldEmail">Old E-mail:</label>
<input id="oldEmail" name="oldEmail" type="text" maxlength="80" value="<?php echo (isset($oldEmail) ? htmlentities($oldEmail) : ''); ?>" /><!-- Sticky Field -->

If your existing HTML files have the .HTML extension the added php script will not work as expected and output your php script as ordinary text.

Changing your HTML file to a .php extension is required for the php script to work.

I think that changing the extension can be globally achieved by using a .htaccess file and await confirmation.

If I didn’t want to rename the .html files, I would edit the httpd.conf file

AddType application/x-httpd-php .php .html

Of course that would mean that all .html files would go through the PHP engine regardless of whether or not they had any PHP code in them. But IMHO the inefficient resource use would likely be negligible.

Or you can do that in the .htaccess file too, if you don’t have access to the httpd.conf file—which you normally wouldn’t on shared hosting.

<!DOCTYPE html>
I had pasted the code below, not sure if its right. When i posted it, the form that the code is shows up. Not the html script. The site wont allow me to actually upload the actual file. What i have is what i wanna add the php code to. I cant get the actual html to show up, i put the script in it just shows what it is for, a submit form

<!DOCTYPE html>
<html>
<body>

<form>
Last Name:<br>
<input type="text" name="lastname">
<br>
First Name:<br>
<input type="text" name="firstname">
<br>
Street Address 1:<br>
<input type="text" name="streetaddress1">
<br>
Street Address 2:<br>
<input type="text" name="streetaddress2">
<br>
City:<br>
<input type="text" name="city">
<br>
State:<br>
<input type="text" name="state">
<br>
Zipcode:<br>
<input type="text" name="zipcode">
<br>
Phone:<br>
<input type="text" name="phone">
<br>
Fax:<br>
<input type="text" name="fax">
<br>
Email:<br>
<input type="text" name="email">
<br>
Verify Email:<br>
<input type="text" name="verifyemail">
<br>
How did you find us?<br>

<input type="radio" name="mailing"value="mailing"/>Mailing<br>
<br>
<input type="radio" name="phonebook"value="phonebook"/>Phonebook<br>
<br>
<input type="radio" name="searchengine"value="searchengine"/>Search Engine<br>
<br>
<input type="radio" name="phonecall"value="phonecall"/>Phonecall<br>
<br>
<input type="radio" name="wordofmouth"value="wordofmouth"/>Word of mouth<br>
<br>
<input type="radio" name="other"value="other"/>Other<br>
<br>
<input type="submit" value="Submit Button"/>


</form>
</body>
</html>

EDIT
This post has been reformatted by enclosing the code block in 3 backticks
```
on their own lines.

I might be missing it, but I don’t see any PHP code in that.

If you mean “how”, it can be messy, but it’s not uncommon to do something like

<input type="text" name="<?php echo $lastname; ?>">

as mikey_w showed previously.

You arent missing it, im just trying to find out how to add php to the html.

It’s easy. Something like this.

<div><?php echo "this is inside the div";?></div>

Run that in a .php file and then view the source to see the output.

The only problem im having is that, because im so new to working with php and html together. I dont know where to place it in the html code

Anywhere. It doesn’t matter.Wherever you put itthe PHP is where it will run in your source code.

You need to pick up a book on basic php and read it. Using php or any programming language is a skill that can’t be summed up on forum posts. Especially if this can be considered your first introduction to programming in general. You’re not going to learn much asking generalized questions about such a vast topic. If you’re serious about learning this stuff head over to learnable and start going through some of the basic php material available there. Having said that do even have PHP installed on the machine your working from. If your not working on a remote server with all this stuff already installed and my guess is not you will need to install it on your machine. PHP and web server typically aren’t available by default on a os.

Try this to see if it works

<!DOCTYPE html>
<html>
<body>
<?php echo "<h1>Hello PHP</h1>"; ?>
<form>
.....

If you don’t see the “Hello PHP”: you’ll need to figure out why.

*Hint you need to place the file in a server folder and go to it in a browser, not your OS file system

I am working with xammp for php

And you can run PHP files OK using it?

Yes, the only problem i am having is that i wanna run my html in the php environment. I add my html to the htdocs in xammp, when i run it the submit form(the html) will run but when i test it by filling the form out nothing happens, its supposed to verify everything you entered… but it doesnt it just stays on the same page and all the boxes i entered information in… eveything goes away and the boxes in the submit form are empty like i never entered anything

I see.

Your form tag is missing attributes.

Try changing it to
<form action="" method="post">
Then at the beginning of the page put

<?php 
if (isset($_POST['lastname'])) {
$lastname = $_POST['lastname'];
} else {
$lastname = "enter last name here";
}
?>

and change

<input type="text" name="lastname">

to

<input type="text" name="<?php echo $lastname; ?>">

That won’t do any validating or sanitization, but if it works you can do similar for the other inputs

Better yet, build your form and at the top of the page put this.

<?php
echo'<pre>'; print_r($_POST); echo'</pre>';
?>

This will show you everything the form posts.
This is probably one of the best ways to debug all types of arrays ($_POST is an array, hence to get the values, $_POST[‘lastname’]), keep this in your wallet.
I have also seen this many times,

$lastname = $_POST['lastname']

My underscores disappeared in $_POST

I wanted to thank everyone for the help i recieved.