CodeIgniter Forums
Loading form values from database when editing versus set_value() for validation - 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: Loading form values from database when editing versus set_value() for validation (/showthread.php?tid=20448)



Loading form values from database when editing versus set_value() for validation - El Forum - 07-10-2009

[eluser]mattpointblank[/eluser]
Hi all.

I'm doing some basic CRUD forms and for my edit page, I want to display the fields stored in the database upon page load. When the user submits the form, I want the validation function to run, and then show them the form again, this time with their (potentially different) $_POST values instead.

This means I can't use the set_value() function as my form field values, otherwise on the first page load, the values from my database aren't present. Similarly, I can't just assign my database fields as the form values either, because then when the form's posted, it will overwrite the $_POST data with the stored data. Confusing!

I've been getting around this using this code at the start of each form view:

Code:
if(!$url = set_value('URL')) { $url = $row['url']; }

Then in the form, I just do:

Code:
<input type="text" name="url" value="<?php echo $url; ?>" />

This works, but I can't help but feel it's a bit messy. Is there a better method for this sort of thing?

Thanks
Matt
[/code]


Loading form values from database when editing versus set_value() for validation - El Forum - 07-10-2009

[eluser]Thorpe Obazee[/eluser]
I can't understand. Why can't you use set_value()?

Code:
<input type="text" class="text" name="post_title" id="post_title" value="<?php echo set_value('post_title', $page->post_title);?>">

In this code, the $page->post_title is the data from the db. When I update, it still loads the data from the db which might be different already.


Loading form values from database when editing versus set_value() for validation - El Forum - 07-10-2009

[eluser]mattpointblank[/eluser]
... and that's what I get for not knowing my functions. Thanks, you just saved me adding a dozen lines of code to every form!