Php adds and modifies the same form

Source: Internet
Author: User

This situation may occur when you write a website. It is to write a form that is used to add an article. We receive data in the background and store it in the database. now there is a problem. what do you do when you want to modify this article?

My method is to make an article in the same form. The following example shows the code I wrote using the Thinkphp framework. If you do not understand Thinkphp, it doesn't matter. I just want to talk about my solution.

The front-end is like this.

Description: The add method submitted to the article controller for receiving.

Is such a simple form,

We receive data directly in the background.

Public function add {

If (IS_POST ){

// You can add the name = "submit" attribute to the button here. Then use if (isset ($ _ POST ['submit ']) to judge post submission.

$ Title = $ _ POST ['title'];

$ Content = $ _ POST ['content'];

If ($ title & $ content ){

// Insert data.

$ Flag = model-> add (post data .);

If ($ flag) $ str = "successful ";

Else $ sttr = "failed ";

} Else {

$ Str = "failed, title or content cannot be blank! ";

$ This-> error ($ str, U ('Article/Index '));

Exit;

}

// Return the operation result.

$ This-> success ("add". $ str, U ('Article/Index'); // jump to the article list page

Exit;

}

$ This-> display (); // if not submitted, our template is displayed.

}

Operation successful failed. Jump Back To The add page. note that I have added an exit structure in some places. this is mainly because the following template file will be displayed if it is not added. because I didn't add else after that if (IS_POST.

// Well, if you want to modify it, we will link it through url, point it to the add method of the article controller, and pass the id to the past, that is, the above method.

In the form, we need to modify it like this.


I added a hidden field to the form. If the variable exists, the id is added.

Background processing page.

Public function add {

If (IS_POST ){

$ Title = $ _ POST ['title'];

$ Content = $ _ POST ['content'];

If ($ title & $ content ){

// Insert data.

// Here I first create an array to put the data

$ Data = array (

'Title' => $ title,

'Cntent' => $ content

);

If ($ id = $ _ POST ['id']) {

// Indicates the id...

$ Flag = Model Modification data;

} Else {

$ Flag = add data to the model;

$ Type = "add ";

}

If ($ flag) $ str = "successful ";

Else $ sttr = "failed ";

} Else {

$ Str = "failed, title or content cannot be blank! ";

$ This-> error ($ str, U ('Article/Index '));

Exit;

}

// Return the operation result.

$ This-> success ($ type. $ str, U ('Article/Index'); // jump to the article list page

Exit;

}

// The template output here should also be noted.

If ($ id = $ _ GET ['id']) {

$ This-> ret = the model queries data through $ id and puts it in the template.

}

$ This-> display (); // if not submitted, our template is displayed.

}


Okay, the logic is messy... let's expand one point below. What if there are attachments or something like that? What should I do. the principle is similar. however, you must determine whether any attachment has been submitted. use if ($ _ FILES ['file'] ['name']) to determine, because if only if ($ _ FILES ['file']) it cannot be determined that an attachment has been submitted. because the array is not empty if no attachment exists, instead, Array ([name] => [type] => [tmp_name] => [error] => 4 [size] => 0. this is true in the if statement.

So we need to add a name to judge...

Okay, I have made a bunch of dregs. Thank you for reading it.

May it helpful to you.

Best Wishes.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.