Jump to content

Recommended Posts

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/267353-populating-form-fields-on-update/
Share on other sites

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.

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'));

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.

 

 

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);

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);

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.

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.