php 在同一个表单中添加和修改
大家写网站的时候可能都会遇到这种情况,就是写一个表单,这个表单是用来添加一篇文章的,我们屁颠屁颠的在后台接收数据,然后存入数据库.现在有个问题.当你要对该文章进行修改的时候,你是怎么处理的?
我的方法是在同一个表单中做文章,下面的例子是我用 Thinkphp框架写的代码,如果你不懂Thinkphp,没有关系,我只是想说说我的解决方案.
前台如果是这样的.
说明,表示提交到article控制器的add方法.进行接收操作.
就是这样一个简单的表单,
我们在后台直接接收数据.
public function add{
if(IS_POST){
//这里其实可以使用在按钮上加一个name="submit"属性.然后使用if(isset($_POST['submit'])来判断post提交.
$title = $_POST['title'];
$content = $_POST['content'];
if($title&&$content){
//插入数据.
$flag = 模型->add(post 过来的数据.);
if($flag) $str = "成功";
else $sttr="失败";
}else{
$str = "失败,标题或者内容不能为空!";
$this->error($str,U('article/index'));
exit;
}
//返回操作结果.
$this->success("添加".$str,U('article/index')); //跳转到文章列表 页
exit;
}
$this->display();//如果不是提交,则显示我们的模板.
}
操作成功失败,跳回添加页面.这里注意一点,我在有些地方添加了一个exit 结构.主要是因为如果不加,会显示下面的模板文件.因为我没有用在那个if(IS_POST)后面加else.
//好,如果要进行修改.我们通过url连接,将其指向article 控制器的add方法,将id传递过去.,也就是上面的方法.
表单中,我们需要这样修改.
我在表单中添加了一个隐藏域.如果有那个变量,则添加该id
后台处理页面.
public function add{
if(IS_POST){
$title = $_POST['title'];
$content = $_POST['content'];
if($title&&$content){
//插入数据.
//这里我先建立一个数组,将数据放入
$data = array(
'title' => $title,
'cntent'=> $content
);
if($id = $_POST['id']){
//表示有id...
$flag = 模型修改数据;
}else{
$flag = 模型添加数据;
$type = "添加";
}
if($flag) $str = "成功";
else $sttr="失败";
}else{
$str = "失败,标题或者内容不能为空!";
$this->error($str,U('article/index'));
exit;
}
//返回操作结果.
$this->success($type.$str,U('article/index')); //跳转到文章列表 页
exit;
}
//这里模板输出我们同样要注意.
if($id = $_GET['id']){
$this->ret = 模型通过$id 查询数据,放入模板中.
}
$this->display();//如果不是提交,则显示我们的模板.
}
好吧,逻辑比较混乱...下面拓展一点,如果有附件之类的呢?该如何处理呢.其实原理类似.只是你必须判断是否有附件提交过来. 使用 if($_FILES['file']['name'])来判断,因为如果只写if($_FILES['file'])是不能判断确实有附件提交过来的.因为如果没有附件,该数组不为空,而是Array ( [name] => [type] => [tmp_name] => [error] => 4 [size] => 0 )这样的内容.这个在if判断语句中当然是为真了.
所以我们要加上名字来判断...
好了,说了一堆渣渣,谢谢你能看完.
May it helpful to you .
Best Wishes.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to implement page jump after PHP form submission [Introduction] In web development, form submission is a common functional requirement. After the user fills out the form and clicks the submit button, the form data usually needs to be sent to the server for processing, and the user is redirected to another page after processing. This article will introduce how to use PHP to implement page jump after form submission. [Step 1: HTML Form] First, we need to write a page containing a form in an HTML page so that users can fill in the data that needs to be submitted.

How to use JavaScript to realize the automatic prompt function of the input box content of the form? Introduction: The automatic prompt function of the form input box content is very common in web applications. It can help users quickly enter the correct content. This article will introduce how to use JavaScript to achieve this function and provide specific code examples. Create the HTML structure First, we need to create an HTML structure that contains the input box and the auto-suggestion list. You can use the following code: <!DOCTYP

How to handle user rights management in PHP forms With the continuous development of web applications, user rights management is one of the important functions. User rights management can control users' operating rights in applications and ensure the security and legality of data. In PHP forms, user rights management can be implemented through some simple code. This article will introduce how to handle user rights management in PHP forms and give corresponding code examples. 1. Definition and management of user roles First of all, defining and managing user roles is a matter of user rights.

How to use JavaScript to implement real-time verification of the input box content of a form? In many web applications, forms are the most common way of interaction between users and the system. However, the content entered by the user often needs to be validated to ensure the accuracy and completeness of the data. In this article, we will learn how to use JavaScript to implement real-time verification of the content of the form's input box and provide specific code examples. Creating the form First we need to create a simple table in HTML

How to use HTML, CSS and jQuery to implement the advanced function of automatic saving of forms. Forms are one of the most common elements in modern web applications. When users enter form data, how to implement the automatic saving function can not only improve the user experience, but also ensure data security. This article will introduce how to use HTML, CSS and jQuery to implement the automatic saving function of the form, and attach specific code examples. 1. Structure of HTML form. Let’s first create a simple HTML form.

PHP form processing: form data query and filtering Introduction In Web development, forms are an important way of interaction. Users can submit data to the server through forms for further processing. This article will introduce how to use PHP to process the query and filter functions of form data. Form design and submission First, we need to design a form that includes query and filter functions. Common form elements include input boxes, drop-down lists, radio buttons, check boxes, etc., which can be designed according to specific needs. When the user submits the form, the data will be sent to POS

Forms are an integral part of writing a website or application. Laravel, as a popular PHP framework, provides rich and powerful form classes, making form processing easier and more efficient. This article will introduce some tips on using Laravel form classes to help you improve development efficiency. The following explains in detail through specific code examples. Creating a form To create a form in Laravel, you first need to write the corresponding HTML form in the view. When working with forms, you can use Laravel

How to create a form with floating prompts using HTML, CSS and jQuery In modern web design, forms are one of the indispensable components. In order to improve user experience, we often need to add some floating prompts to the form to guide users to fill in the form correctly. This article will introduce how to use HTML, CSS and jQuery to create a form with floating prompts, and provide specific code examples. First, we need to create the HTML form. In the form we need to add some input fields, and
