首先,我正在使用 codeigniter 框架,我是 JS 和 AJAX 的初学者,所以请多多包涵。
我读了this问题,所以我试着按照答案。
这是脚本(已更新):
$(function() {
$( "#datepicker").datepicker().click(function() {
$.ajax({
type: 'POST',
url: "<?php echo base_url(); ?>backend/umat/add",
dataType: "json",
data: $('#form_daftar').serialize(),
success: function(data) {
console.log("Done");
}
});
return false;
});
});
这是我的日期选择器 HTML 代码:
<input type="text" id="datepicker" name="datepicker"
value="<?php echo isset($umat['tanggal_lahir'])?$umat['tanggal_lahir']:""?>"/>
我的问题是(已更新):
- 我在上面的 AJAX 中提供的 URL 是否正确?
- 我应该如何将数据从 AJAX 传递到 PHP (
url: "<?php echo base_url(); ?>backend/umat/add"
)?
谢谢你的帮助:D
最佳答案
你在这里错过了什么:
- 您的点击事件在 doc ready 处理程序之外,应该在 doc ready 内部,这样当页面准备就绪时,元素应该可供点击。
- 您错过了点击事件的结束
});
标记。(不过没关系)
所以试试这个:
$(function() {
$( "#datepicker").datepicker();
$("#datepicker").click(function() {
$.ajax({
type: 'POST',
url: "<?php echo base_url(); ?>backend/umat/add",
dataType: "json",
data: {frmData : $('#form_daftar').serialize()}, // <----send this way
success: function(data) {
console.log(data.date);
// here data is the returned data from the specified url and make sure
// that url is generating a proper json structrue.
// suppose there is a key named date which holds the submitted date so
// data.date will get you the date.
}
});
}); //<----you missed this closing of click function.
}); //<----------put everything before this closing of doc ready handler.
虽然你也可以像这样链接它:
$(function(){
$( "#datepicker").datepicker().click(function() {
//......ajax function in click here
});
});
关于php - 如何在 php/codeigniter 中为日期选择器执行 $_POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16557231/