Friday, April 19, 2013

working with json in codeigniter



  <?php
$attributes = array("id"=>"register_form");
echo form_open('register',$attributes); ?>
<div id="w4l-reg-name" class="control-group">
<div class="controls">
<input type="text" placeholder="Full Name" name="name">
<span class="help-inline"></span>
</div>
</div>

<div id="w4l-reg-number" class="control-group">
<div class="controls">
<input type="text" placeholder="Favorite Number" name="number">
<span class="help-inline" ></span>
</div>
</div>

<div id="w4l-reg-email" class="control-group">
<div class="controls">
<input type="text" placeholder="Email Address" name="email">
<span class="help-inline"></span>
</div>
</div>

<div id="w4l-reg-email2" class="control-group">
<div class="controls">
<input type="text" placeholder="Confirm Email Address" name="email2">
<span class="help-inline"></span>
</div>
</div>

<div id="w4l-reg-password" class="control-group">
<div class="controls">
<input type="password" placeholder="Password" name="password">
<span class="help-inline"></span>
</div>
</div>
<div id="w4l-reg-password2" class="control-group">
<div class="controls">
<input type="password" placeholder="Confirm Password" name="password2">
<span class="help-inline"></span>
</div>
</div>
       
      </div>
      <div class="modal-footer">
        <button type="button" onclick="memberRegister();" class="btn btn-danger">Sign Up</button>
        </form>
---------------------------------------------------------------------------------------------------------



function memberRegister(){
$.post(BASE_PATH+"register/index",$("#register_form").serialize(), function (data){
$('#w4l-reg-name').removeClass('error');
$('#w4l-reg-name span').html('');
$('#w4l-reg-number').removeClass('error');
$('#w4l-reg-number span').html('');
$('#w4l-reg-email').removeClass('error');
$('#w4l-reg-email span').html('');
$('#w4l-reg-email2').removeClass('error');
$('#w4l-reg-email2 span').html('');
$('#w4l-reg-password').removeClass('error');
$('#w4l-reg-password span').html('');
$('#w4l-reg-password2').removeClass('error');
$('#w4l-reg-password2 span').html('');


var resp = null;
try {
resp = $.parseJSON(data);
} catch (e){
alert('Communication error. Please try again later' );
return;
}
if(typeof(resp.errors) != 'undefined') {
for(var i in resp.errors) {
if(resp.errors[i] != '') {
$('#'+i).addClass('error');
$('#'+i+' span').html(resp.errors[i]);
}
}
} else {
alert(resp.msg);
location.href=BASE_PATH;
}
});
}
-------------------------------------------------------------------------------------------------



      function index()
    {
        $this->form_validation->set_rules('name', 'Full Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('email', 'Email Address', 'trim|xss_clean|required|valid_email|matches[email2]|callback_emailCheck');
        $this->form_validation->set_rules('email2', 'Confirm Email Address', 'trim|xss_clean|required|valid_email');
        $this->form_validation->set_rules('number', 'Favorite Number', 'trim|required|xss_clean|is_natural|less_than[100]');
        $this->form_validation->set_rules('password', 'Password', 'trim|xss_clean|required|matches[password2]');
        $this->form_validation->set_rules('password2', 'Confirm Password', 'trim|xss_clean|required');
        if ($this->form_validation->run() == FALSE)
        {
$err = array();
$err['w4l-reg-name'] =  form_error('name');
$err['w4l-reg-email'] =  form_error('email');
$err['w4l-reg-email2'] =  form_error('email2');
$err['w4l-reg-number'] =  form_error('number');
$err['w4l-reg-password'] =  form_error('password');
$err['w4l-reg-password2'] =  form_error('password2');

echo json_encode(array('errors' => $err));
        }
        else
        {
            $this->user_model->add_user();
$success = array("msg"=>"You have successfully added a new user!");
echo json_encode($success);
        }
    }

No comments:

Post a Comment