Welcome Guest, Not a member yet? Register   Sign In
How to re-populate the form with values from the database?
#1

[eluser]Yanny[/eluser]
Hi all,

I know how to re-populate the form with post data, via set_value('fieldname');
Now I want to re-populate the form with post data again but the post data contains data from the database. How to do that?

Normally without using framework I will do something like this:

Code:
//when there are not post values do this
if (!isset($_POST)) {
  //get data from the database with a sql query
  $rs = mysql_query("The query goes here");

  //put the data in the post variables
  $row = mysql_fetch_assoc($rs);
  $_POST['username'] = $row['username'];
  $_POST['email'] = $row['email'];
}

//display the form with post data
<form>
*Username: <input type="text" name="username" value='<?php echo $_POST['username'];?>' /><br />
*Email: <input type="text" name="email" value='<?php echo $_POST['email'];?>' />
</form>

So how to do this now with CodeIgniter?

I have tried something like this:
Code:
$this->input->post('username') = $data['user']['username'];
$data['user']['username'] is a value from the database, but it does not work.

I know you guys know how to do it, since this is a common thing. Thanks in advance!
#2

[eluser]Higher Ground Studio[/eluser]
I would use ajax to submit the form and return json or xml O(from your server side validation script) to a js function that then adds it to the form values. Pretty easy to do.
#3

[eluser]Yanny[/eluser]
Is there a way without using ajax?
#4

[eluser]cahva[/eluser]
set_value() will take second parameter which you can use to populate the fields if data has not yet been sent. With that you can set the default value if the value is not posted yet. Something like this..

Controller:
Code:
function index()
    {
        $this->load->model('users');
        $this->load->helper('form');
        
        // Get userinfo from model and set it to $data['userinfo']
        $data['userinfo'] = $this->users->get_userinfo();
        
        $this->load->view('user',$data);
    }

View:
Code:
<?php echo form_open('user') ?>
*Username: &lt;input type="text" name="username" value="&lt;?php echo set_value('username',$userinfo-&gt;username) ?&gt;" /><br />
*Email: &lt;input type="text" name="email" value="&lt;?php echo set_value('email',$userinfo-&gt;email) ?&gt;" />
&lt;?php echo form_close() ?&gt;
#5

[eluser]Yanny[/eluser]
Thank you so much cahva! That's what I need! Maybe I better read the user guide before asking... haha..
#6

[eluser]BandonRandon[/eluser]
This a great, but how may you suggest doing the same thing with Checkboxs and Radios? I am storing the correct vaule as a number in my database.
#7

[eluser]Unknown[/eluser]
I suggest the following method for filling the form fields with database values.

Code:
if(!empty($_POST)){

       //just for understanding
       $_POST['user_email'] = $_POST['user_email'];

}else{
       // Set post vars with db values
       $data['user'] = $this->users_model->get_user($user_id);
       $_POST['user_email'] = $data['user']['user_email'];
}

in this way, the form is filled with the DB data, or when the user changed the data, submitted, but a error occured, the form is filled again but now with the data from the POST.




Theme © iAndrew 2016 - Forum software by © MyBB