简体   繁体   中英

Post Variables not being passed by jQuery Ajax Request?

So what I am trying to do is use a full calendar ajax call to get the event registered in the database. When I try to do that, no error pops up, infact, the ajax call returns successful addition to the the database.

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.

This is the ajax call in default.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='+ title+'&start='+ start +'&end='+ end +'&url='+ url ,
                 type: "POST",
                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.)

  <?php
   // Values received via ajax

   $title = $_POST['title'];
   $start = $_POST['start'];
   $end = $_POST['end'];
   $url = $_POST['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. They are just not going from the jQuery to php.

Any assistance how that could be achieved would be appreciated.

在此处输入图片说明

use

data: { title : title, start : start , end : end , url : url } 

instead of

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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