When you write a website, you may encounter this situation, is to write a form, this form is used to add an article, we fart in the background to receive data, and then into the database. Now there's a problem. What do you do when you want to make changes to the article?
My method is to make a fuss in the same form, the following example is the code I write with the thinkphp framework, if you do not understand thinkphp, no matter, I just want to talk about my solution.
If this is the front desk.
<form method= "POST" action= "{: U (' Article/add ')} >
<input type= "text" name= "title"/>
<textarea name= "Content" ></textarea>
<input type= "Submit" name= "Submit"/>
</form>
Description that represents the Add method that is submitted to the article controller. Receive operation.
Is such a simple form,
We receive data directly in the background.
Public Function add{
if (is_post) {
You can actually use the button to add a name= "submit" property. Then use if (Isset ($_post[' submit ')) to determine the POST submission.
$title = $_post[' title '];
$content = $_post[' content '];
if ($title && $content) {
Insert data.
$flag = Model->add (post coming data.);
if ($flag) $str = "Success";
Else $sttr = "Failed";
}else{
$str = "failed, title or content cannot be empty!";
$this->error ($str,U (' Article/index '));
Exit
}
Returns the result of the operation.
$this->success ("Add". $str, U (' Article/index ')); Jump to the article List page
Exit
}
$this->display ();//If it is not submitted, our template is displayed.
}
The operation failed successfully and jumped back to the Add page. Note here that I have added an exit structure in some places. Mainly because if not added, the following template file is displayed. Because I'm not using the IF (is_post) behind and else.
OK, if you want to make a change. We connect it via a URL, point it to the article controller's Add method, and pass the ID to the past.
form, we need to modify this.
<form method= "POST" action= "{: U (' Article/add ')} ><!--//action commit address unchanged .-->
<input type= "text" name= "title" Value= "{$ret. Title}"/><!--//Add content .-->
<textarea name= "Content" >{$ret .content}</textarea>
<if condition= "$ret" >
<input type= "hidden" name= "id" value= "{$ret. ID}"/><!--//If there is an article variable, add a hidden field id-->
</if>
<input type= "Submit" name= "Submit"/>
</form>
I added a hidden field to my form. If you have that variable, add the ID
Background processing page.
Public Function add{
if (is_post) {
$title = $_post[' title '];
$content = $_post[' content '];
if ($title && $content) {
Insert data.
Here I first set up an array to put the data into
$data = Array (
' title ' = $title,
' Cntent ' = $content
);
if ($id = $_post[' id ')} {
Indicates that there is an ID ...
$flag = model modification data;
}else{
$flag = model add data;
$type = "Add";
}
if ($flag) $str = "Success";
Else $sttr = "Failed";
}else{
$str = "failed, title or content cannot be empty!";
$this->error ($str,U (' Article/index '));
Exit
}
Returns the result of the operation.
$this->success ($type. $str,U (' Article/index ')); Jump to the article List page
Exit
}
Here we also need to pay attention to the template output.
if ($id = $_get[' id ')} {
$this->ret = model queries the data through $id and puts it into the template.
}
$this->display ();//If it is not submitted, our template is displayed.
}
Well, the logic is quite confusing ... What if there is an attachment or something like that? How to deal with it. The principle is similar. Just you have to decide if there is an attachment to submit it. Use if ($_files[' file ' [' name ']) to determine, because if only write if ($_files[' file ') is not able to determine that there is indeed an attachment submitted. Because if there is no attachment, the array is not empty, but array ([name] = > [Type] = [Tmp_name] + [ERROR] = 4 [size] + 0) Such content. This is certainly true in the IF judgment statement.
So we have to add a name to judge ...
Well, said a heap of slag, thank you to read it.
May it helpful.
Best Wishes.