You may encounter this situation when you write a website, is to write a form that is used to add an article, we fart in the background to receive data, and then into the database. Now there's a question. What do you do when you want to revise the article?
My method is to make a fuss in the same form, the following example is the code I wrote with the thinkphp framework, if you don't understand thinkphp, it doesn't matter, I just want to talk about my solution.
Front desk If this is the case.
Description that represents the Add method submitted to the article controller. Receive operation.
It's such a simple form,
We receive data directly in the background.
Public Function add{
if (is_post) {
Here you can actually use the button to add a name= "submit" attribute. Then use if (Isset ($_post[' submit ')) to determine the POST submission.
$title = $_post[' title '];
$content = $_post[' content '];
if ($title && $content) {
Inserts data.
$flag = Model->add (post over 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 Story list page
Exit
}
$this->display ();//If not submitted, display our template.
}
Operation failed, skip back to add page. Note here that I have added an exit structure in some places. The main reason is that if you do not add, the following template file is displayed. Because I'm not using the IF (is_post) to add else after that.
OK, if you want to make changes. We connect by URL, point it to the article controller's Add method, pass the ID to the past.
form, we need to modify this.
{$ret. Content}
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) {
Inserts data.
Here I first set up an array to put the data into
$data = Array (
' title ' => $title,
' Cntent ' => $content
);
if ($id = $_post[' id ']) {
Indicates an ID ...
$flag = model modification data;
}else{
$flag = model to 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 Story list page
Exit
}
Here the template output we should also note.
if ($id = $_get[' id ']) {
$this->ret = model queries the data by $id and puts it into the template.
}
$this->display ();//If not submitted, display our template.
}
Well, the logic is rather confusing ... Next, if there are attachments and the like? How to deal with it. In fact, the principle is similar. It's just that you have to decide if there are any attachments submitted. Use if ($_files[' file '] [' name ']) to determine, because if you write only if ($_files[' file '), you cannot tell if an attachment has been submitted. Because if there is no attachment, the array is not empty, but the arrays ([name] => [ Type] => [tmp_name] => [ERROR] => 4 [size] => 0. This is, of course, true in the IF judgment statement.
So we have to add names to judge ...
Well, I said a bunch of slag, thank you for reading it.
May it helpful.
Best Wishes.