3

I have a form with few fields. Now on clicking a button "save as draft", the ajax call will go and the poplulated fields will be stored in the database.

But when the user clicks on "save as draft" again, then this time the previous field should be updated and not a new field be created.

So I thought some remedy

-. Making a hidden field and load it with unique number when the page is loaded and also store it in SESSION variable.

And after comparing the unique number with the session variable, if the number is same then update the field in sql or else create a new field in sql.

Is the above solution ok or people use different one?

Santosh
  • 1,871
  • 1
  • 19
  • 36
  • 1
    Please add code to show what your situation is. – Jim Martens Jul 26 '13 at 18:00
  • This is not a code problem I am asking how to determine if the draft has to be updated or a new one be created. I wrote one solution which I thought, but I wanted to learn what other people do. – Santosh Jul 26 '13 at 18:02
  • 1
    I'm working on a project that has a similar feature. I use the hidden id field and identify it that way. – Paul Dessert Jul 26 '13 at 18:03
  • 1
    Well than this is probably the wrong expectation. SO is for solving specific problems, not collecting opinions. So I'd recommend you that you reword your question to something that actually expects a solving answer. – Jim Martens Jul 26 '13 at 18:04
  • If I ask code then people will think I am asking them to do my work. I just want the method, the standard process. Whom should I ask then what is standard process to know if the previous field was already in sql database or not? – Santosh Jul 26 '13 at 18:06
  • @relentless Do you store the hidden field in session too? But what if the user has two pages open. Will there be no conflict? – Santosh Jul 26 '13 at 18:11

3 Answers3

3

Why not, on the first Ajax call to 'save draft' retrieve back some very simple JSON such as

{ field_id: 1 }

And then save that save that value either in the DOM by placing it in an input, or better - just store it in a variable. You could then check for this variable's existence on subsequent draft / final saves and change your Ajax post to account for it. You could just have an additional Boolean column in your table such as final which tracks the stage of the post. From the server side perspective, you could easily get the value of the MySQL insert by calling MySQLi's insert_id() method e.g.

A really nice additional touch might also involve using the pushState method to update the URL (e.g. to index.php?draft=1). That way, even if the user clicked the back button, they could go back to the correct page, and you could pull their saved data for them.

Community
  • 1
  • 1
Ian Clark
  • 9,237
  • 4
  • 32
  • 49
1

1 Solution = Add the fields values to a DB table = compare the values when submitted again, if your value matched then no change else change is made so you can update mysql.

2 Solition = Other solution could be to store data in Sessions and when user has done editing it add them to DBase tbl.

3rd solution Store data to any flat file system and then use it to add in mysql table.

4th solution Allow edit field in form so the user can alter the data if he want to edit it at any time.

Ahmed Habib
  • 189
  • 11
  • solution 1: why he needs to compare and then update? better would be directly run the update query. Wh 2 operation if the same can be achieved by one? – Shivang Agarwal Sep 15 '20 at 13:30
  • Solution 2: saving in session is not a good approach as any case user won't make the final action he will lose his data. for example, he saves the draft twice but didn't make the final one in which system will persist the data in database? – Shivang Agarwal Sep 15 '20 at 13:33
  • Solution 3: I think store in database would be better approach rather then saving into flat file system – Shivang Agarwal Sep 15 '20 at 13:34
1

If you have an auto-incremented field in your database, you can get the id of that field. When the user saves as a draft, you can store the change in the database, and store the id of the record in the session. Then, when the user saves as a draft again, you can use that id to update the right row in the database.

You could store the id in the form, but then the user could easily (maybe even accidentally) post data using the wrong id and update the wrong row.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • If the user has two or more pages open, then storing the row id with session create no conflict? Is there a way to circumvent this difficulty? – Santosh Jul 26 '13 at 18:17
  • You could store it in the form as well. It's not a big risk, but it is a risk. You could also store the actual ID in an array in the session and store it in the form as well. That way, you have the right ID in the form, but you can still validate against the session if an ID is still being edited and is not outdated. – GolezTrol Jul 26 '13 at 18:23