有人能帮我写代码吗?我有AJAX json类型的POST,我想在代码点火器中获得一两个数据发送到控制器,我想添加条件函数,其中数据来自json。
我的JSON
$('#add_people').submit(function(e){
e.preventDefault();
var id_people = $('#id_people').val();
var name_people = $('#name_people').val();
var phone_people = $('#phone_people').val();
$.ajax({
type : "POST",
url : "<?php echo base_url('add_people_data')?>",
dataType : "JSON",
data : {id_people:id_people , name_people:name_people, phone_people:phone_people},
success: function(data){
$("#show_people").html('<img src="assets/img/user/spinner.gif"> <h4>Loading data...</h4>');
$('#modal-add_people').modal('hide');
$('.modal-backdrop').remove();
$('[name="id_people"]').val("");
$('[name="name_people"]').val("");
$('[name="phone_people"]').val("");
var loadUrl = "<?php echo base_url('show-people-data')?>";
$("#show_people").load(loadUrl);
}
});
return false;
});
我的控制器
public function add_people_data()
{
$id_people = $this->input->post('id_people');
$name_people = $this->input->post('name_people');
$phone_people = $this->input->post('phone_people');
$cekassignreviewer=$this->Model_reviewer->checkassignreviewer('data_people', $id_people, $name_people, $phone_people);
if ($cekassignreviewer > 0) {
$this->session->set_flashdata('failed',
"<script>swal({
title:'Failed!',
text: 'Number of people has been added',
icon: 'error',
confirmButtonText: 'Ok',
confirmButtonClass: 'btn btn-success'
});
</script>");
redirect(base_url('setting-reviewer'));
}else{
$data=$this->Model_reviewer->add_people();
echo json_encode($data);
}
我的型号
function add_people()
{
$data = array(
'id_people' => $this->input->post('uniid_reviewer'),
'name_people' => $this->input->post('name_people'),
'phone_people' => $this->input->post('phone_people'),
);
$result=$this->db->insert('data_people',$data);
return $result;
}
提前感谢
CodeIgniter 4的IncomingRequest类
如果您不在控制器内,但仍需要访问应用程序的Request对象,则可以通过Services类获得该对象的副本:
$request = ConfigServices::request();
使用CodeIgniter的内置方法,您可以简单地执行以下操作:
$something = $request->getVar('foo');
getVar()
方法将从$_REQUEST
中提取,因此将返回来自$_GET
、$_POST
或$_COOKIE
的任何数据。虽然这很方便,但您通常需要使用更具体的方法,如:
$request->getGet()
$request->getPost()
$request->getServer()
$request->getCookie()
此外,还有一些实用的方法可以从$_GET
或$_POST
检索信息,同时保持控制您查找信息的顺序的能力:
$request->getPostGet() - checks $_POST first, then $_GET
$request->getGetPost() - checks $_GET first, then $_POST