Welcome Guest, Not a member yet? Register   Sign In
Loading form values from database when editing versus set_value() for validation
#1

[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]
#2

[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.
#3

[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!




Theme © iAndrew 2016 - Forum software by © MyBB