SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    SitePoint Evangelist
    Join Date
    Apr 2009
    Location
    South Carolina
    Posts
    458
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Each day is a learning experience.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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 include and require 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?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •