php - 如何在 php/codeigniter 中为日期选择器执行 $_POST

标签 php javascript jquery ajax

首先,我正在使用 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']:""?>"/>

我的问题是(已更新):

  1. 我在上面的 AJAX 中提供的 URL 是否正确?
  2. 我应该如何将数据从 AJAX 传递到 PHP ( url: "<?php echo base_url(); ?>backend/umat/add" )?

谢谢你的帮助:D

最佳答案

你在这里错过了什么:

  1. 您的点击事件在 doc ready 处理程序之外,应该在 doc ready 内部,这样当页面准备就绪时,元素应该可供点击。
  2. 您错过了点击事件的结束 }); 标记。(不过没关系)

所以试试这个:

    $(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
   });
});

See a demo here

关于php - 如何在 php/codeigniter 中为日期选择器执行 $_POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16557231/

相关文章:

php - 检测到路径错误的 Apache 服务

javascript - 将 steam 应用列表加载为 JSON 文件

javascript - 即使定义了函数后,onclick 属性也不起作用

带有拼写检查的 Javascript RTE

php - 如何查看相册中的图片

php - php 的函数参数

javascript - 将div设置为容器div的末尾

javascript - 将相同的类和事件绑定(bind)到多个动态生成的元素

JQuery 输入动态检查

javascript - jQuery 的 CSS3 效果仅适用于 chrome 调试