表单输入
<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发送
$(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);
}
});
});
发送到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);
}
如何在codeigniter
每次添加数据的情况下插入ajax
会话,
结果如:
[0] =&gt;排列 ( [nama] =&gt;测试 [email] =&gt;测试 )
[1] =&gt;排列 ( [nama] =&gt; testtt [email] =&gt; testttt )
[2] =&gt;排列 ( [nama] =&gt; AAA [email] =&gt; AAAAA )
[3] =&gt;排列 ( [nama] =&gt; AA [email] =&gt; sdfaaaaasdfdsf )
答案 0 :(得分:0)
我不知道codeinteger如何获取/设置用户数据,但试试这个
$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);
将数据传递给脚本的最安全的方法是
var datastring = $("#form").serialize();
$.ajax({
type: "POST",
url: "your url.php",
data: datastring,
success: function(data) {
alert('Data send');
}
});