Welcome Guest, Not a member yet? Register   Sign In
Optional Default Value
#1

I am new to CodeIgniter, I love it! So simple!
I'm getting to know the basics so I'm creating a user management system.
Logging in, out and registering all works fantastic.

I am currently working on the editing of the profile.
I want the default value of the input fields to be the session variable of first_name and last_name however if the form has been posted I want the default value to be the post variable instead.

This is my application/views/profile/edit.php.
PHP Code:
<div class="container">
    <
div class="panel panel-default">
          <
div class="panel-heading">Edit Profile</div>
              <
div class="panel-body">
                  <
div class="row">
                      <
div class="col-xs-5">
                        <?
php echo validation_errors(); ?>
                           <?php echo form_open('profile/edit'); ?>
                        <div class="form-group">
                            <label for="first_name">First Name:</label>
                            <input type="text" class="form-control" id="first_name" name="first_name" value="<?php echo set_value('first_name'$this->session->userdata('logged_in')['first_name']); ?>" />
                        </div>
                        <div class="form-group">
                             <label for="last_name">Last Name:</label>
                            <input type="text" class="form-control" id="last_name" name="last_name" value="<?php echo set_value('last_name'$this->session->userdata('logged_in')['last_name']); ?>" />
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-primary" value="Edit Profile" />
                        </div>
                        </form>
                    </div>
                </div>
        </div>
        <div class="panel-footer text-right">Page rendered in <strong>{elapsed_time}</strong> seconds!</div></div>
    </div>
</div> 

I hope I have made this easy enough to understand,
Thank you in advance!
Reply
#2

You could create a $first_name and $last_name variable before the form that takes the POST data if available, else take the session data. This would be for if you refresh the same page on form submit.

Something like this
PHP Code:
<?php
$first_name 
= ($this->input->post('first_name')) ?: $this->session->userdata('logged_in')['first_name']; 
$last_name  = ($this->input->post('last_name')) ?: $this->session->userdata('logged_in')['last_name']; 
?>
<div class="container">
    <div class="panel panel-default">
          <div class="panel-heading">Edit Profile</div>
              <div class="panel-body">
                  <div class="row">
                      <div class="col-xs-5">
                        <?php echo validation_errors(); ?>
                           <?php echo form_open('profile/edit'); ?>
                        <div class="form-group">
                            <label for="first_name">First Name:</label>
                            <input type="text" class="form-control" id="first_name" name="first_name" value="<?php echo set_value('first_name'$first_name); ?>" />
                        </div>
                        <div class="form-group">
                             <label for="last_name">Last Name:</label>
                            <input type="text" class="form-control" id="last_name" name="last_name" value="<?php echo set_value('last_name'$last_name); ?>" />
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-primary" value="Edit Profile" />
                        </div>
                        </form>
                    </div>
                </div>
        </div>
        <div class="panel-footer text-right">Page rendered in <strong>{elapsed_time}</strong> seconds!</div></div>
    </div>
</div> 
Reply
#3

Is set_value() not working for you? I've looked at the code, and it seems that, the first time through, the value displayed would be $this->session->userdata('logged_in')['first_name']) and only on second and subsequent displays would be value come from the value entered in the field. Odd.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#4

(This post was last modified: 02-28-2015, 07:40 PM by SomeGuy.)

This is how I handle "form field population", taylored to your specific case. Make of it what you will.

View file:

Code:
<input type="text" name="field_name" value="<?php echo htmlentities($post['field_name']) ?>">

In your controller...
Code:
...
function my_method() {
  $template_data = array(
    'post' => array(
      'field_name' => $this->session->userdata('logged_in')['first_name'],
    ),
  );

  if($this->input->post()) :
    $template_data['post'] = array_merge($template_data['post'], $this->input->post());
    // process form;
  endif;


  // render view with template variables
  $this->load->view('view-file', $template_data);
}

...
Reply
#5

(02-28-2015, 01:00 PM)silentium Wrote: You could create a $first_name and $last_name variable before the form that takes the POST data if available, else take the session data. This would be for if you refresh the same page on form submit.

Something like this


PHP Code:
<?php
$first_name 
= ($this->input->post('first_name')) ?: $this->session->userdata('logged_in')['first_name']; 
$last_name  = ($this->input->post('last_name')) ?: $this->session->userdata('logged_in')['last_name']; 
?>
<div class="container">
    <div class="panel panel-default">
          <div class="panel-heading">Edit Profile</div>
              <div class="panel-body">
                  <div class="row">
                      <div class="col-xs-5">
                        <?php echo validation_errors(); ?>
                           <?php echo form_open('profile/edit'); ?>
                        <div class="form-group">
                            <label for="first_name">First Name:</label>
                            <input type="text" class="form-control" id="first_name" name="first_name" value="<?php echo set_value('first_name'$first_name); ?>" />
                        </div>
                        <div class="form-group">
                             <label for="last_name">Last Name:</label>
                            <input type="text" class="form-control" id="last_name" name="last_name" value="<?php echo set_value('last_name'$last_name); ?>" />
                        </div>
                        <div class="form-group">
                            <input type="submit" class="btn btn-primary" value="Edit Profile" />
                        </div>
                        </form>
                    </div>
                </div>
        </div>
        <div class="panel-footer text-right">Page rendered in <strong>{elapsed_time}</strong> seconds!</div></div>
    </div>
</div> 

Thank you, mate. I used your method, slightly differently.
I'm trying to keep the as much if/else statements out of the view files so I added to the controller.
PHP Code:
        $data = array(
            
"first_name" => (!$this->input->post('first_name'))?$this->session->userdata('first_name'):$this->input->post('first_name'),
            
"last_name" => (!$this->input->post('last_name'))?$this->session->userdata('last_name'):$this->input->post('last_name')
        ); 
Reply
#6

(03-01-2015, 06:08 AM)miiikkeyyyy Wrote: Thank you, mate. I used your method, slightly differently.
I'm trying to keep the as much if/else statements out of the view files so I added to the controller.

Adding the if/else to the controller is definitely the right way to do go. Just added it above you HTML for simplicity in showing how to do it Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB