简体   繁体   English

需要帮助jQuery将数据发布到php文件

[英]Need assistance with jQuery posting data to php file

So what I am trying to do is use a full calendar ajax call to get the event registered in the database. 所以我想做的是使用完整的日历ajax调用来将事件注册到数据库中。 When I try to do that, no error pops up, infact, the ajax call returns successful addition to the the database. 当我尝试执行此操作时,实际上没有错误弹出,ajax调用将成功添加到数据库。

I tested the PDO query separately and it is working perfect, so kindly assist me with the ajax post, because that is what I have short listed the error to. 我分别测试了PDO查询,它运行良好,因此请协助我处理ajax帖子,因为这就是我将错误简短列出的内容。

This is the ajax call in default.html. 这是default.html中的ajax调用。

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='+ title+'&start='+ start +'&end='+ end +'&url='+ 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');
}

And this is the add_events.php (Kindly note that this has no error except that it is not receiving the post.) 这就是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` "
  . "SET "
  .   "`title`   = :title, "
  .   "`start`   = :start, " 
  .   "`end`     = :end, " 
  .   "`url`     = :url ";

     $stmt   = $pdo->prepare($sql);
     $result = $stmt->execute(array(':title'=>$title, ':start'=>$start, ':end'=>$end,  ':url'=>$url));
    if (!$result) {
        print_r($pdo->errorInfo());

    }   
?>

I see xhr GET request in the console log with post variables. 我在控制台日志中看到带有发布变量的xhr GET请求。 They are just not going from the jQuery to php. 他们只是不从jQuery到php。

Any assistance how that could be achieved would be appreciated. 可以实现的任何帮助将不胜感激。

The JQuery Pop up after adding an event says : 添加事件后弹出的JQuery显示:

PHP Error : Undefined Index (for all variables) PHP错误:未定义索引(对于所有变量)

The variables set from jQuery are not passing to the php. 从jQuery设置的变量没有传递给php。

The Header of main file(default.html) looks like this : 主文件的标题(default.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>

The console is giving the following error :- POST http://localhost/calendar/add_events.php 500 (Internal Server Error) 控制台显示以下错误:-POST http://localhost/calendar/add_events.php 500(内部服务器错误)

Here is the error in the network tab :- http://postimg.org/image/4k9ztzuep/ 这是“网络”标签中的错误: -http : //postimg.org/image/4k9ztzuep/

Based on the answer provided by delighted I was able to get the query working and resolving the jQuery error. 基于delighted提供的答案,我能够使查询正常工作并解决jQuery错误。

But the POST does not work :- I say this because I changed all fields to allow NULL but than it does allow NULL and creates a NULL, NULL, NULL,NULL entry in the database. 但是POST无效:-之所以这样说是因为我将所有字段都更改为允许NULL,但是它不允许NULL,并在数据库中创建了NULL,NULL,NULL,NULL条目。

Based on your images of the request, you need to set dataType:'text', in your ajax call. 根据您的请求图像,您需要在ajax调用中设置dataType:'text',

Also, in your PHP, change your PDO to: 另外,在您的PHP中,将PDO更改为:

// "?"s here will get replaced with the array elements below
$sql = 'INSERT INTO  evenement ( title,start,end,url) VALUES( ?, ?, ?, ? )';
$stmt = $db->prepare($sql);
$stmt->execute(array($title, $start, $end, $url )); // these array elements will replace the above "?"s in this same order

// check for errors
if($stmt->errorCode() == 0) {
    $id = $db->lastInsertId(); // get the id form the inserted record, not really needed but I put it here to show you how

}
else {
    // had errors
    $errors = $stmt->errorInfo();
    print_r($errors);
}

Additionally,you should always encode your inputs when building your data string especially since you are passing dates and a URL . 此外,在构建数据字符串时,应始终对输入进行编码, 尤其是要传递日期和URL时

Change your code to: 将您的代码更改为:

data: 'title='+ encodeURIComponent(title)+
      '&start='+ encodeURIComponent(start)+
      '&end='+ encodeURIComponent(end)+
      '&url='+ encodeURIComponent(url),

Note, even if this doesn't solve your current issue, you should still implement this change ;) 请注意,即使这不能解决您当前的问题,您仍然应该实施此更改;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM