carlg Posted August 21, 2012 Share Posted August 21, 2012 I'm trying to figure out how to populate my form fields on a simple crud update. Here is some of my view (I use this view for both adds and updates): <input type="text" name="email" value="<?php echo set_value('email', ''); ?>"/> Here is the code in the controller that loads the above view: function update($id){ $data['seller'] = $this->seller_model->get_seller_byID($id)->row(); $this->load->view('EditSeller', $data); } This all works fine for me on adds, but how do I populate the form fields for an edit? I needs to happen somewhere in the update function of my controller; I'm just not sure how. This is most likely a simple question, but I'm just starting CI, thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/ Share on other sites More sharing options...
Christian F. Posted August 21, 2012 Share Posted August 21, 2012 Just fetch the ID for the record you want to change, and set it using a hidden field or the URL. Then use that ID to run an update statement. Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1371005 Share on other sites More sharing options...
carlg Posted August 21, 2012 Author Share Posted August 21, 2012 Maybe I wasn't clear enough on the question. I'm trying to set the values when the form is loaded for the first time. I'm using the same form/view for both adds and updates. I'm using the set_value in the form to display the fields. The user clicks an edit button from a summary page which calls the update function you see above passing in the id of the seller to update. Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1371043 Share on other sites More sharing options...
Mahngiel Posted August 21, 2012 Share Posted August 21, 2012 CI's set_value() is for the validator's use. What you need is a good ol' HTML solution... Either use the value for a submit-able response, or use html5's placeholder for hints. <?php $data = array( 'name' => 'email', 'size' => 30, 'value' => $user->email, 'placeholder' => 'email address' ); echo form_input($data, set_value('email')); Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1371063 Share on other sites More sharing options...
carlg Posted August 21, 2012 Author Share Posted August 21, 2012 Does this allow me to use the same form/view for adds and updates? Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1371070 Share on other sites More sharing options...
Mahngiel Posted August 21, 2012 Share Posted August 21, 2012 why wouldn't it? Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1371075 Share on other sites More sharing options...
carlg Posted August 21, 2012 Author Share Posted August 21, 2012 What about when the user submits the form and there are validation errors? Would this hold the values in the fields so the user does not need to enter them again like set_value and validation does? Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1371084 Share on other sites More sharing options...
Mahngiel Posted August 21, 2012 Share Posted August 21, 2012 You can see i provided set_value(), right? Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1371113 Share on other sites More sharing options...
carlg Posted August 21, 2012 Author Share Posted August 21, 2012 I think i see now. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1371183 Share on other sites More sharing options...
carlg Posted August 25, 2012 Author Share Posted August 25, 2012 Sorry, I had another question about this topic so I reopened it. In the case where I'm using my view for an add, $data is undefined. So do you think it makes sense to throw in a $data=""; in the add part of my controller before the view gets called? And when it's doing an update, it will do something similar to what Mahngiel posted. Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1372386 Share on other sites More sharing options...
carlg Posted August 25, 2012 Author Share Posted August 25, 2012 Actually when I do what I said in the previous post, it comes back with an error when the user submits an erroneous form since the $data variable does not get kept on the re-rendering of the view. Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1372388 Share on other sites More sharing options...
Mahngiel Posted August 26, 2012 Share Posted August 26, 2012 i use $data for generic one-use statements. An example of this would be multiple form inputs on a single page. $data = array( 'name' => 'email', 'size' => 30, 'class'=>'input'); echo form_input($data, set_value('name') ); $data = array( 'name' => 'password', 'size' => 30, 'class'=>'input'); echo form_password( $data ); $data is never defined in the scope of your controller and nor should it be. it's purely a single-use variable. You shouldn't need to pass anything from the controller to it either. If you need to add values to your form inputs, simply add a key into the $data object and put the value as your reference object. $data = array('name' => 'email_update', 'value' => $user->email); Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1372420 Share on other sites More sharing options...
carlg Posted August 26, 2012 Author Share Posted August 26, 2012 So in my view I have this: (keep in mind I'm using the same view for adds and updates) <?php echo form_input($fname, set_value('fname')); ?> <?php echo form_input($email, set_value('email')); ?> When the user clicks the button to update here is the controller code before the view gets called: $data['name'] = array( 'fname' => 'fname', 'value' => $seller->s_fname, 'placeholder' => 'First Name' ); $data['email'] = array( 'name' => 'email', 'size' => 50, 'value' => $seller->s_email, 'placeholder' => 'Email' ); and then $data is passed to the view (I turned your $data into an array of multiple elements) When the user wants to add a new record, he clicks the add button and here is the code from the controller: $data['title'] = 'Add new seller'; $data['message'] = ''; $data['action'] = site_url('seller/addSeller'); $data['fname']=""; $this->load->view('EditSeller', $data); Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1372436 Share on other sites More sharing options...
carlg Posted August 26, 2012 Author Share Posted August 26, 2012 So where I am going wrong with the above. Anyway, please excuse me if I'm not getting it that quick as I'm newer to CI. Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1372445 Share on other sites More sharing options...
Mahngiel Posted August 26, 2012 Share Posted August 26, 2012 So where I am going wrong with the above. You're mixing logic and presentation. Why? and how does $data['name'] become $fname ? Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1372481 Share on other sites More sharing options...
carlg Posted August 26, 2012 Author Share Posted August 26, 2012 "and how does $data['name'] become $fname ?" I changed it to a separate array for each field on my form. Bad idea? Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1372526 Share on other sites More sharing options...
Mahngiel Posted August 26, 2012 Share Posted August 26, 2012 Yes, "bad idea". Your controller should only contain the logic required to give the view that information is needs to present itself. Where I think you're getting confused is using PHP as a programming language and using PHP as a HTML meta-language. CodeIgniter is one of the very few frameworks that doesn't ship with a template system because it uses libraries to ease the templating process. This is likely where you're getting mixed up. So, if you're controller is for a user's profile, your controller should ONLY build the user object. That's it. No more. $user = $this->users_model->get_user($username); $user->friends = $this->users_model->get_friends($user->user_id); //reference global CI object $this->data->user =& $user; //load view $this->load->view( 'user_profile', $this->data ); You then use the existing $data object (or array if that's how u roll) to supplement your templates // let's figure out who's profile this is if( $user->user_id === $this->session->userdata(user_id) ) echo heading('My Profile', 4); else echo heading($user->username . "'s Profile",4); // here's a reusable variable being used to create a change email form that we will prepopulate $data = array( 'name' => 'email', 'size' => 30, 'class'=>'input', 'value' => (isset($user->email) ? $user->email : '' ) ); echo form_input($data, set_value('email') ); The user object only interacts with the CI's php-based templating system, you don't need to create it in your controller. Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1372601 Share on other sites More sharing options...
carlg Posted August 26, 2012 Author Share Posted August 26, 2012 I think what I was missing was where the $data is located. Looks like you are setting it up and using it in the view. I was setting it up in the controller and trying to pass it to the view. I'm going to try to put this together later in the afternoon today (or evening). Thanks for all your help so far!!! Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1372608 Share on other sites More sharing options...
carlg Posted August 26, 2012 Author Share Posted August 26, 2012 Got it now. What I mentioned above was the problem! Quote Link to comment https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/#findComment-1372621 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.