Welcome Guest, Not a member yet? Register   Sign In
form_validation with JSON
#1

Hi!

I'm the beginner with CI and I want to create form with form_validation() and JS.
This is my code:

CONTROLLER:
PHP Code:
...
 public function 
add_wolont()
 {
// template...

 
$this->load->model'Wolontariusze' );

 if ( !empty( 
$_POST ) )
 {

 
$this->load->library('form_validation');
 
 
$this->form_validation->set_rules('imie_wolont''Imie wolontariusza''trim|required');
 
$this->form_validation->set_rules('d_imie_wolont''Drugie mie wolontariusza''trim|required');
// etc. 

 
if ($this->form_validation->run() === FALSE) {

 echo 
json_encode(array('st' => 0,'msg' => 'ERROR: <br>' validation_errors()));

 } else {
 
$dodaj_wolont = array(
 
'imie' => $this->input->post'imie_wolont' ),
 
'drugie_imie' => $this->input->post'd_imie_wolont' ),
// etc.
 
);


 
$str "ADDED!";
 echo 
json_encode(array('st' => 1'msg' => $str));
 }

 }


 } 

VIEW:
Code:
<div id="error"></div>
<?php echo form_open('panel/add_wolont', array('id'=>'dodaj_wolont')) ?>

       <label>Imie</label>
        <input type="text" name="imie_wolont">

       <label>Drugie imie</label>
        <input type="text" name="d_imie_wolont">

       <label>Nazwisko</label>
        <input type="text" name="nazwisko_wolont">


<input type="submit" value="Dodaj">
<?php echo form_close(); ?>

JS:
Code:
$(function() {
     $('#dodaj_wolont').submit(function() {
           $.post($('#dodaj_wolont').attr('action'),$('#dodaj_wolont').serialize(),function(json) {
                  if ( json.st == 0 ) {
                        alert(json.msg);
                  } else {
                        alert(json.msg);
                  }
            }, 'json');
            return false;
      });
});

In console all is fine, but alerts doesn't work and I don't know why  Sad
Can anyone help?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB