Reputation: 125
I am new to the codeigniter and i am trying its tutorials from the user guide. But i just got stuck in one place. In the code of the form validations. I am trying its example for the set_value property but my user text box is not returning anything.
In my view i added the following code:
<?php echo validation_errors();?>
<?php echo form_open('form');?>
<table border="0">
<tr>
<td><h5>Username</h5></td>
<td><input type="text" name="username" value="" size="50" value="<?php echo set_value('username'); ?>" /></td>
</tr>
<tr>
<td><h5>Password</h5></td>
<td><input type="password" name="password" value="" size="50" accept=" <?php echo set_value('password'); ?>" /></td>
</tr>
<tr>
<td><h5>password Confirm</h5></td>
<td><input type="password" name="passconf" value="" size="50" /></td>
</tr>
<tr>
<td><h5>Email Addres</h5></td>
<td> <input type="text" name="email" value="" size="50" accept="<?php echo set_value('email'); ?>" /></td>
</tr>
<tr>
<td> </td>
<td><div><input type="submit" value="Submit" /></div></td>
</tr>
</table>
</form>
and in my controller i added the following code:
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username','username','required|min_length[5]|max-length[10]|xss_clean');
$this->form_validation->set_rules('password','password','required');
$this->form_validation->set_rules('passconf','password confirmation','required');
$this->form_validation->set_rules('email','email','required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsucces');
}
}
Thank You in advance.
Upvotes: 1
Views: 3164
Reputation: 120
This solution is working, best answer. By the way, what is the difference between
<?php echo set_value('field_name'); ?>
and
<?php echo @$_POST['field_name']; ?>
Upvotes: 0
Reputation: 401
There are some problems with CI and form validation...
You need own helper where grabs a value from the POST array for the specified field so you can re-populate an input field or textarea. If form validation is active it retrieves the info from the validation class
if (!function_exists('set_value')) {
function set_value($field = '', $default = '') {
$OBJ = & _get_validation_object();
if ($OBJ === TRUE && isset($OBJ->_field_data[$field])) {
return form_prep($OBJ->set_value($field, $default));
} else {
if (!isset($_POST[$field])) {
return $default;
}
return form_prep($_POST[$field]);
}
}
}
Upvotes: 0
Reputation: 5108
There are two value attributes in your input type..
Try this...
<td><input type="text" name="username" size="50" value="<?php echo set_value('username'); ?>" /></td>
Upvotes: 3