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).
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:
it outputs nothing but only gather required data and calls a template:
template.phpwhich is your main site template,consists of your header and footer:
and calls the actual page template:
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:
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.