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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With