0

我正在使用 JQuery Full Calendar 来显示来自我的 mysql localhost 的 JSON 提要的结果。最初它使用 更新日历功能events: "json-events.php",,但我也尝试json-events.php使用 POST 方法和提交按钮从表单上的提交按钮运行。虽然 JSON 提要效果很好,但我只能得到结果的 ECHO,并且无法返回 index.php 以在日历上显示结果。json-events.php 的代码...

<?php

$year = date('Y');
$month = date('m');
$day = date('d');

$link = mysql_connect('localhost','root','');
if (!$link) { die('Could not connect: ' . mysql_error()); }
mysql_select_db('jauntUK');

$latevent = $row['Lat'];
$lngevent = $row['Long'];
$tgtlat = $_POST['searchlat'];
$tgtlatstring = number_format($tgtlat, 16, '.', '');
$tgtlng = $_POST['searchlong'];
$tgtlngstring = number_format($tgtlng, 16, '.', '');


if($_POST) {


    $result = mysql_query("SELECT *, ( 3959 * acos( cos( radians( `Lat` ) ) * cos( radians(" . $tgtlatstring . ") ) * cos( radians(" . $tgtlngstring . ") - radians( `Long` ) ) + sin( radians( `Lat` ) ) * sin( radians(" . $tgtlatstring . ") ) ) ) AS distance FROM EventData HAVING distance < 5")
or die (mysql_error());

// Initializes a container array for all of the calendar events
$jsonArray = array();

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    var_dump($row);

 $eventtitle = $row['Title'];
 $eventdate = $row['StartDate'];
 $eventend = $row['EndDate'];
 $eventallday = $row['FullDay'];
 $eventURL = $row['URL'];
 $Description = $row['Description'];
 $Address1 = $row['Address1'];
 $Address2 = $row['Address2'];
 $Address3 = $row['Address3'];
 $Address4 = $row['Address4'];
 $PostCode = $row['PostCode'];
 $Admission = $row['Admission'];

 // Stores each database record to an array
 $buildjson = array('title' => "$eventtitle", 'start' => "$eventdate", 'end' => "$eventend", 'allday' => "$eventallday", 'eventURL' => "$URL", 'description' => "$Description", 'Address1' => "$Address1", 'Address2' => "$Address2", 'Address3' => "$Address3", 'Address4' => "$Address4", 'PostCode' => "$PostCode", 'Admission' => "$Admission");

 // Adds each array into the container array
 array_push($jsonArray, $buildjson);
}
// Output the json formatted data so that the jQuery call can read it
echo json_encode($jsonArray);   
header('location: index.php');


 } else { 


 echo json_encode(array(

        array(
            'id' => 001,
            'title' => "Search for events near you",
            'start' => "$year-$month-$day",

        )

    ));
}
?>

有什么建议吗?- 在此先感谢您的帮助

4

1 回答 1

0

您是否要保留旧事件并添加新事件?还是只显示新事件?如果是前者,您可以尝试使用 AJAX 而不是提交表单并重定向回 index.php。您可以返回成功的数据并使用http://arshaw.com/fullcalendar/docs/event_rendering/renderEvent/ 来呈现它。像这样的东西

('#submit-button').on("click",function(){

  $.ajax({
        type:"POST",
        url: "json-events.php",
        data: $('#your-form').serialize(),
        success: function(data){
                        $.each(data, function(index, event){
                          $('#calendar').fullCalendar('renderEvent', event);
                        );
        }
    });
    return false;  // to stop the form from actually submitting
});

您需要在日历页面上的文档就绪功能内的任何位置添加此代码。当在您的表单上单击提交按钮时,它会将数据从您的表单发送到您的 json-events.php 页面。在那里您查询您的数据库并使用 json_encode 返回事件。然后在成功函数中呈现您刚刚从数据库中提取的每个事件。我从fullcalendar js中借用了上面的示例:使用 ajax 获取更多事件不知道它是否有效,但您可以看到这个想法。

如果您不想显示旧事件,您可以查看添加

('#calendar').fullCalendar('removeEvents');

在添加新的之前。

于 2012-09-25T00:02:28.907 回答