POST 变量没有将日期从 jQuery 提交到 php


POST variable is not submitting dates from jQuery to php

我正在尝试将fullcalendar集成到我的php应用程序中。

但是,我在将它们保存到数据库中时遇到问题,数据库不接受标题和 url,除非它们在双引号中。

开始日期和结束日期仍为 NULL。因此,当我刷新时,数据会使用开始和结束时间元素的 null 值添加到数据库中。这引起了人们的关注,因为当我刷新页面时,所有事件都会从日历中消失,而实际上它们仍在数据库中。

当我调整新插入事件的空变量的值时,所有事件都会再次出现。

这是默认的 ajax 调用.html。

selectable: true,
selectHelper: true,
select: function(start, end, allDay) {
    var title = prompt('Event Title:');
    var url = prompt('Type Event url, if exits:');
    if (title) {
         var start = $.fullCalendar.moment(start);
         var end = $.fullCalendar.moment(end);
         console.log("Event Triggered");
         $.ajax({
                 url: 'add_events.php',
                 data: 'title='+ encodeURIComponent(title)+
                       '&start='+ encodeURIComponent(start)+
                       '&end='+ encodeURIComponent(end)+
                       '&url='+ encodeURIComponent(url),
                 type: "POST",
                 dataType: 'text',
                success: function(json) {
                    alert('Added Successfully');
                 },
                error: function(xhr, textStatus, errorThrown) {
                     alert(xhr.responseText);
                }
         });
        calendar.fullCalendar('renderEvent',
        {
             title: title,
             start: start,
             end: end,
             url:url,
             allDay: allDay
        },
            true // make the event "stick"
        );
    }
    calendar.fullCalendar('unselect');
}

这是add_events.php(请注意,除了它没有收到帖子外,这没有错误。

  <?php
   // Values received via ajax
        $data = $_POST;
  $json_array["title"]=json_decode($data["title"], true);
  $json_array["start"]=json_decode($data["start"], true);
  $json_array["end"]=json_decode($data["end"], true);
  $json_array["url"]=json_decode($data["url"], true);

  $title =$json_array["title"];
  $start =$json_array["start"];
  $end = $json_array["end"];
  $url = $json_array["url"];
  // connection to the database
 try {
  $bdd = new PDO('mysql:host=localhost;dbname=fullcalendar', '....',    '.......');
 } catch(Exception $e) {
   exit('Unable to connect to database.');
 }

   $sql = 'INSERT INTO  `evenement` ( `title`,`start`,`end`,`url`) VALUES( ?, ?, ?, ? )';
   $stmt = $pdo->prepare($sql);
   $stmt->execute(array($title, $start, $end, $url )); 
  // check for errors
   if($stmt->errorCode() == 0) {
       $id = $pdo->lastInsertId(); 
   }
   else {
  // had errors
        $errors = $stmt->errorInfo();
        print_r($errors);
    }
?>

如能提供任何援助,将不胜感激。

主文件(默认.html)的标头如下所示:

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
 <link href='css/fullcalendar.css' rel='stylesheet' />
 <link href='css/fullcalendar.print.css' rel='stylesheet' media='print' />
 <script src='js/moment.min.js'></script>
 <!--<script src='js/jquery.min.js'></script>-->
 <script src='js/fullcalendar.min.js'></script>

但是开机自检不适用于日期:-我这样说是因为我更改了所有字段以允许 NULL,但它确实允许 NULL 并在数据库中创建一个 NULL、NULL、NULL、NULL 条目。

有很多事情可能会出错,所以让我们从基础知识开始。

步骤1:- 检查$_POST变量是否正在接收预期数据。如果日期没有达到php那么你知道你必须摆弄前端js

    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    die();

步驟二:- 如果日期反映在$_POST数组中,则日期格式可能不匹配,特别是如果数据库中日期列的数据类型设置为 DATETIME,因为它希望日期采用"Y-m-d H:i:s"格式。

  $start =date("Y-m-d H:i:s", strtotime($json_array["start"]));
  $end =date("Y-m-d H:i:s", strtotime($json_array["end"]));

注意:- 这只是算法代码,尚未准备好生产,因为需要进行大量检查以确保没有抛出警告/错误。 例如,在将日期字符串传递给函数之前检查它是否不为空strtotime()

步骤3:- 也许尝试echo正在执行的mysql查询等。

现在你正在这样做:

data: 'title='+ title+'&start='+ start +'&end='+ end +'&url='+ url , `

这不是在POST上发送参数的正确方法。这看起来更像您在参数附加到 url 的GET上所期望的。相反,创建一个对象并将其传递给数据字段:

var obj = {
   title: title,
   start: start,
   end: end,
   url: url
}

然后:

data: obj