0

form input

<form id='form'>
  <input type="text" name="nama" value="">      
  <input type="text" name="email" value="">     
  <input type="button" id='button' name="button" value="kirim">
</form>
<div id='hasil'></div>

ajax sent

$(document).on('click', '#button', function(event) {
  event.preventDefault();       
  var data = $('#form').serializeArray();
  $.ajax({
    data : {data : data},
    url:'data/test_ajax',
    method:'post',
    success:function (data_ajax){
      console.log(data_ajax);
    }
  });
});

sent to data/test_ajax

public function test_ajax(){
    $data = $this->input->post('data');

    $get_session = $this->session->userdata('data');
    for ($i=0; $i < 1000; $i++) { 
        if ($get_session[$i]=="") {
            $dat['data'][$i]['nama'] = $data[0]['value'];
            $dat['data'][$i]['email'] = $data[1]['value'];
            $set_session = $this->session->set_userdata($dat);
            break;
        }
    }

    $get_session = $this->session->userdata('data');
    print_r($get_session);
}

how to insert to session with codeigniter every adding data from ajax, result like as :

[0] => Array ( [nama] => test [email] => test )

[1] => Array ( [nama] => testtt [email] => testttt )

[2] => Array ( [nama] => aaa [email] => aaaaa )

[3] => Array ( [nama] => aa [email] => sdfaaaaasdfdsf )

3
  • You check actual session data, but overwrite it with new values Commented Mar 24, 2017 at 2:57
  • yes, why overwrite it new values in new row array ? @bato3 Commented Mar 24, 2017 at 3:02
  • You may want to remove the name attribute on your button, because I don't think you want/need $_POST['button]='kirim' Commented Mar 24, 2017 at 6:28

1 Answer 1

0

I dont ktow how works codeinteger get/set-userdata, but try this

$get_session = $this->session->userdata('data');
$get_session[] = Array('nama'=>$data[0]['value'], 'email' => $data[1]['value']);
$set_session = $this->session->set_userdata('data',$get_session);

OT safest way to pass data to script would be

var datastring = $("#form").serialize();
    $.ajax({
        type: "POST",
        url: "your url.php",
        data: datastring,
        success: function(data) {
             alert('Data send');
        }
    });
Sign up to request clarification or add additional context in comments.

1 Comment

@Maxthinks this seems like the correct answer. If you are satisfied please award it the green tick so that your question is removed from the unanswered questions list. If this answer is not quite doing what you need, please leave a comment for bato3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.