如何在PHP应用程序上管理页面/表单? [英] How to manage pages/forms on PHP application?

查看:76
本文介绍了如何在PHP应用程序上管理页面/表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,并试图编写简单的在线商店。

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>

我创建了文件 add_category.php和 add_product.php,它们包含html表单。他们正在工作,但我想在内容div中显示该表格,并将数据保存在数据库中,但我遇到了问题。我想应用全局的页眉,页脚等,并且不想将索引文件的布局复制到应用程序的每种形式。

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.

我不知道:


  • 如何在我的页面中显示不同的页面(现在我正在尝试使用$ _POST ['action的开关分支进行管理']数据,但我不知道这是最好的方法)

  • 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)

我应该从哪里发送数据?我的意思是中的动作属性。换句话说-我应该在index.php

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

处理应该保持数据不变的数据的地方有关PHP的书籍,但没有一本书说明如何构建应用程序。它们都是关于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.

我需要有关管理表单/页面的简单模式/示例/文章并在它们之间进行通信(例如通过POST)。试图向Wordpress来源学习,但这对我来说有点太复杂了(大型复杂的注册表,对象缓存等)。

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.).

我没有编程问题一般而言(IM Windows应用程序程序员)。

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

推荐答案

与其将脚本包含在主站点模板中,还不如说是相反-将模板包含到PHP脚本中。

您可以分别处理脚本或创建单个入口点。两种方法相差不大,但我建议使用前一种方法作为入门。

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.

示例布局如下:
a页面:

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 这是您的主要网站模板,

template.php which is your main site template,

由页眉和页脚组成:

<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

其主要思想是用户友好的表单处理和业务逻辑/显示逻辑分离,同时保持最自然的网站布局-se为不同的站点模块分配文件,但设计通用。

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.

这篇关于如何在PHP应用程序上管理页面/表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆