Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:39:23+00:00 2026-05-30T08:39:23+00:00

im new in PHP and im trying to write simple online shop. Lets say

  • 0

im new in PHP and im trying to write simple online shop.

Lets say have index file like this:

<?php  
require_once '/inc/db.php';
$db = new Db();
?>  

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
        <div id="header"><?php require_once("header.php"); ?></div>

        <div id="content">
                <?php
                    // i have no idea what to put here
                ?>
        </div>

        <div id="footer"><?php require_once("footer.php"); ?></div>
</body>
</html>

I created files “add_category.php” and “add_product.php”, they contain html forms. They are working, but i want to display that form in content div, and save data in database and i have problem. I want to apply my global header, footer etc. and i don’t want to copy my layout from index file to each form of my application.

I have no idea:

  • how to display diffrent “pages” in my (now im trying to manage with switch branching of $_POST[‘action’] data, but i don’t know is this best way)

  • where i should “send” data from froms? i mean “action” attribute in . other words – where i should process data keeping constant layout which is “defined” in index.php

I have some books about PHP, but there is no book that says how to build applications. They all are about PHP language and simple tasks on single pages/files.

I need simple pattern/example/article about managing forms/pages and communicate between them (by POST for example). Tried to learn from WordPress sources, but this is a little too complicated to analyze for me (big complicated registry, object caching etc.).

I HAVE NO PROBLEMS WITH PROGRAMMING IN GENERAL (im windows app programmer).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:39:24+00:00Added an answer on May 30, 2026 at 8:39 am

    Instead of including your scripts into main site template you have to do quite contrary – include your templates into PHP scripts.
    You may address your scripts separately or make a single entry point. Both methods doesn’t differ too much, but I’d suggest to use former one for starter.

    So, make your site consists of pages, page templates and main template.
    Once your script called, it have to process data, and then load site template, which, in turn, will load page template.

    An example layout is going to be like this:
    a page:

    <?
    //include our settings, connect to database etc.
    include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
    //getting required data
    $DATA=dbgetarr("SELECT * FROM links");
    $pagetitle = "Links to friend sites";
    //etc
    //and then call a template:
    $tpl = "links.tpl.php";
    include "template.php";
    ?>
    

    it outputs nothing but only gather required data and calls a template:

    template.php which is your main site template,

    consists of your header and footer:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>My site. <?=$pagetitle?></title>
    </head>
    <body>
    <div id="page">
    <? include $tpl ?>
    </div>
    </body>
    </html>
    

    and calls the actual page template:

    <h2><?=$pagetitle?></h2>
    <ul>
    <? foreach($DATA as $row): ?>
    <li><a href="<?=$row['link']?>" target="_blank"><?=$row['name']?></a></li>
    <? endforeach ?>
    <ul>
    

    For the form it is good idea to make form’s action the very same page (so, you can leave form action simply blank).
    The request would be sent to the same page, at the top of which you may put a code like this:

    <?  
    if ($_SERVER['REQUEST_METHOD']=='POST') {  
    
      $err = array();
      //performing all validations and raising corresponding errors
      if (empty($_POST['name']) $err[] = "Username field is required";  
      if (empty($_POST['text']) $err[] = "Comments field is required";  
    
      if (!$err) {  
        //if no errors - saving data and redirect
        header("Location: ".$_SERVER['PHP_SELF']);
        exit;
      }  else {
        // all field values should be escaped according to HTML standard
        foreach ($_POST as $key => $val) {
          $form[$key] = htmlspecialchars($val);
        }
    } else {
      $form['name'] = $form['comments'] = '';  
    }
    $tpl = "form.tpl.php";
    include "template.php";
    ?>  
    

    it will process your data and either redirect to the same page on success or show the filled form on error

    the main idea of this is user-friendly form handling and business logic/display logic separation while keeping most natural site layout – separate files for the different site modules yet common design.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple PHP script which automatically sets up new etherpads
I am new to PHP and am trying to write a simple function that
I am new to php and trying to write a login function. Bit stuck
I'm new to PHP and trying to get my head around security. I have
I am new to php and I am getting this error trying to load
I have a new install of Ubuntu and trying to use the php's mail()
I'm trying to write a simple php function that will strip everything between two
I'm trying to write a php script that will generate a variety of new
experts. I'm trying to create a simple file downloader (with GUI). I'm pretty new
I am new in PHP and trying to save input to a text file,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.