简体   繁体   中英

Cant receive data to php variables from ajax

I've a function of Fullcalendar plugin which should insert in mysql bbdd some data which is transfered by ajax to php.

select: function(start, end, allDay) {
            var title = prompt('Evento a insertar:');
            if (title) {
                start = $.fullCalendar.moment(start).format('YYYY-MM-DD hh:mm:ss');
                end = $.fullCalendar.moment(end).format('YYYY-MM-DD hh:mm:ss');

                 $.ajax({
                     url: 'http://localhost/test-fullcalendar/php/add_evento.php',
                     data: {"title": title, "start": start, "end": end},
                     type: "POST",
                     success: function(json) {
                        alert('OK');
                        console.log(title + ' ' + start + ' ' + end);
                     }
                 });
                 calendar.fullCalendar('renderEvent',
                 {
                     title: title,
                     start: start,
                     end: end,
                     allDay: allDay
                 },
                 true
                 );
            }
            calendar.fullCalendar('unselect');
        }

And my php receive it:

$title = $_POST['title'];
$start = $_POST['start'];
$end = $_POST['end'];

The problem comes when php told me that variables are "undefined index" . I try putting behind an isset() but echo dont show anything so the insert to the mysql table dont runs correctly.

Whats the problem? I dont have experience with Ajax and I think it has relation with the success function because I read that there isn't a particular way to receive data in php from ajax, only the classic method POST..

Thanks

Your Ajax structure is correct, I think the problem is the data that you are posting to php script. Try to use console.log(title + ' ' + start + ' ' + end); before $.ajax . Probably one or more data is incorrect. One of the possible problem is $.fullCalendar.moment().format() . Probably this function does not return a simple string, therefore you can't use it in your ajax data. Try to use console.log() for "start", "end" and "title" separately.

Try to remove the double quotes from the line where you pass data in the AJAX call, like this

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

I think you just need to debug a little bit more.

Navigate to your developers tools 'Network' tab and see what are you sending

网络标签

If everything you are sending is OK, then we can continue making some modifications to make the code comunicate more to you.
For example, we could start from the success callback:

select: function(start, end, allDay) {
            var title = prompt('Evento a insertar:');
            if (title) {
                start = $.fullCalendar.moment(start).format('YYYY-MM-DD hh:mm:ss');
                end = $.fullCalendar.moment(end).format('YYYY-MM-DD hh:mm:ss');
                console.log('Sending:', title, start, end);
                 $.ajax({
                     url: 'http://localhost/test-fullcalendar/php/add_evento.php',
                     data: {"title": title, "start": start, "end": end},
                     type: "POST",
                     success: function(json) {
                        var res = JSON.parse(json);
                        if(res.result === 'OK') {
                            alert('OK');
                        }
                        else {
                            console.error(res.message);
                        }

                     }
                 });
                 calendar.fullCalendar('renderEvent',
                 {
                     title: title,
                     start: start,
                     end: end,
                     allDay: allDay
                 },
                 true
                 );
            }
            calendar.fullCalendar('unselect');
        }

Now, in the PHP part:

<?php
//Check existance
if(!isset($_POST['title']) || !isset($_POST['start']) || !isset($_POST['end'])) {
    echo json_encode(array('result'=>'KO','message'=>'Invalid input! '.var_export($_POST, true)));
    exit;
}


//Check emptyness
if(empty($_POST['title']) || empty($_POST['start']) || empty($_POST['end'])) {
    echo json_encode(array('result'=>'KO','message'=>'Empty input! '.var_export($_POST, true)));
    exit;
}

//title, start and end exists in POST and are not empty

try {
//SQL code, insert/update blablabla
}
catch(Exception $e) {
   echo json_encode(array('result'=>'KO','message'=>$e->getMessage()));
   exit;
}
//....

//All went ok
echo json_encode(array('result'=>'OK'));

Now, with this code, you will be able to know what is happening to your php, and see the errors in the javascript console.

我有您问题的答案,只需将数据中的start和end变量更改为start.format()和end.format()

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