Very Newbie Question

I have been using existing php/mysql forms at work to create more of the same forms…lots of copy and paste. These were originally done by a Unix admin who was learning php on the fly. All the forms work correctly, but all the php code is mixed in with the html. Can php be put in external files similar to what can be done with css? If anyone has read the Sitepoint book “Build Your Own Database Driven Web Site Using PHP and MySQL” Is this topic covered?
Thanks,
Bob

Bob,

You can include PHP into your HTML, or you can have the PHP echo the HTML. If you have code that you use repeatedly it is a good idea to have that in a separate PHP file so you can edit once for the entire site. I started using PHP by including PHP files for the footers on my site.

What I am doing for my forms is to have all the PHP at the top of the file and then have the HTML form at the bottom of the file. As an example, my log-in file PHP checks that the form was submitted, and if not, it skips the remainder of the PHP and displays the HTML form. When the form is submitted the same PHP check now runs the sanitize of each field and verifies with the database that the log-in credentials are correct and if so, creates session information, and if not offers a link to try again to log-in.

Lots of copy/paste is usually a bad sign, but generally speaking each form you create is unique in several ways.

But essentially, yes you can include files much as you do with CSS files.

Look at the [fphp]include[/fphp] and [fphp]require[/fphp] functions.

Most developers use these methods a lot in especially to maximise the re-use of code.

Maybe you could post some examples of what you mean – is it the form elements, PHP form validation or mysql storage that you suspect could benefit from less code duplication?