Prepopulating form data - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Prepopulating form data (/showthread.php?tid=15600) |
Prepopulating form data - El Forum - 02-09-2009 [eluser]macleodjb[/eluser] This is probably a lame question but i want to make sure that i do this correctly with code igniter. I want to pre-populate form data using data from my sql database. I have validation rules set up inside my controller and it will populate the fields when the validation is triggered. How can i intermingle the two without having a duplicate string placed in the value of a field? Prepopulating form data - El Forum - 02-09-2009 [eluser]TheFuzzy0ne[/eluser] This is how I usually do it: Code: class Homepage extends Controller { Basically, the controller will keep loading so long as the validation fails. When the validation is successful, the user is redirected, which prevents them accidentally resubmitting the form when using the back button, or refreshing a page. Ceveat: This method relies on cookies to keep the user posted, although it's possible to do it without the need for cookies easily enough. I hope this helps. Prepopulating form data - El Forum - 03-02-2009 [eluser]ajcolburn[/eluser] This method is great for prepopulating an update record form. I have tried to modify it to allow for the creation of new records, but the system throws a PHP error if the set_value() default variable is not defined. The variable is not defined because if a record for the given ID is not returned by the model then my controller assumes a new record is being created. Basically I'm trying to write the simplest/shortest bit of code to add and edit records with one controller and one view. Any help would be much appreciated... Regards, Alex Prepopulating form data - El Forum - 03-02-2009 [eluser]TheFuzzy0ne[/eluser] If we could see your code it would really help. Prepopulating form data - El Forum - 03-02-2009 [eluser]JayTee[/eluser] [quote author="ajcolburn" date="1236055161"]Basically I'm trying to write the simplest/shortest bit of code to add and edit records with one controller and one view. Any help would be much appreciated... Regards, Alex[/quote] I've had issues with this since I started using CI - but there's just no "easy" way to have a form that can be used for both adding AND editing. I just use 2 forms; 1 for adding, the other for editing. I wish I could be more help. The nice thing is how much quicker/cleaner the CI 1.7 code is for creating these forms. Prepopulating form data - El Forum - 03-02-2009 [eluser]TheFuzzy0ne[/eluser] Here's some untested HTML which might give you some inspiration, but as JayTee said, it's not really an "easy" way: Code: <html> Basically, the rule is, if an id is specified, then the user obviously exists, therefore we must be editing. If not, we're adding. Simple, huh? Prepopulating form data - El Forum - 03-06-2009 [eluser]The Wizard[/eluser] I've made a tutorial on this topic, search for 6 minutes tutorial and you shall find what you are looking for. (do i sound like a dungeon master?) |