Using jquery.load to populate form after failed validation with ajax in codeigniter -


i have seen lot of form validation tutorials jquery ajax. found same thing telling sucessfull form validation. need find how can display errors if server side validation fails. in other words, when submit form without ajax form re-populated error messages , inputs filled user provided. in method use great php functions form_prep. here view code.

<?php // change css classes suit needs     $attributes = array('class' => '', 'id' => 'login'); echo form_open(site_url.'user/insertuser', $attributes);  ?>  <p> <label for="username">user name <span class="required">*</span></label> <input type="text" name="username" maxlength="255" value="<?php echo form_prep($this->input->post('username'));?>"  /> <span id="username"><?php echo form_error('username'); ?></span><br /> </p> <p> <label for="first_name">first name <span class="required">*</span></label> <input type="text" name="first_name" maxlength="255" value="<?php echo form_prep($this->input->post('first_name'));?>"  /> <span id="first_name"><?php echo form_error('first_name'); ?></span><br /> </p>  <p> <label for="last_name">last name <span class="required">*</span></label> <input type="text" name="last_name" maxlength="255" value="<?php echo form_prep($this->input->post('last_name')); ?>"  /> <span id="last_name"><?php echo form_error('last_name'); ?></span><br /> </p>  <p> <label for="password">password <span class="required">*</span></label> <input type="text" name="password" maxlength="255" value="<?php echo form_prep($this->input->post('password')); ?>"  /> <span id = "password"><?php echo form_error('password'); ?></span><br /> </p>  <p> <label for="email">email <span class="required">*</span></label> <input type="text" name="email" maxlength="255" value="<?php echo form_prep($this->input->post('email')); ?>"  /> <span id = "email"><?php echo form_error('email'); ?></span><br /> </p>   <p> <?php echo form_submit( 'submit', 'submit', 'id="submit"'); ?> </p>  <?php echo form_close(); ?> 

you can see here used form_prep($this->input->post('username')) helpful security issue. when use same form re-populate ajax on failed validation not display errors nor input boxes filled. instead loads form if first time loaded. here how using ajax.

first in div loading form

$('#userform').load('<?php $this->load->view('user_form')?>'); 

now ajax

$(function() {     $('#login').submit(function()     {         $.ajax(         {             type : 'post',             data : $(this).serialize(),             url  : $(this).attr('action'),             success : function(data)             {                 if(data == 1)                 {                     //                  }                 else                 {                     $('#userform').load('<?php $this->load->view('user_form')?>');                 }             }         });         return false;     }); }); 

and controller

function insertuser() {     if($this->form_validation->run() == false)     {         echo 0;     }else{         echo 1;     } } 

what render form in controller here instead of

echo 0;   $html =   $this->load->view('user_form',true); echo $html; 

and in ajax function replace

else {     $('#userform').load('<?php $this->load->view('user_form')?>'); } 

with

else  {     $('#userform').html(data); } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -