问题描述
我正在用PHP和JavaScript制作一个小日历应用程序.要填充第一页上的事件div
,我运行以下函数:
I'm making a little calendar app, in PHP and JavaScript. To populate the events div
on the first page, I run this function:
function populateEvents() {
$.ajax({
type: "GET",
url: "populateEvents.php",
success: function(data, textStatus, XMLHttpRequest) {
$('#events').html(data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Status: " + textStatus + ". Error thrown: " + errorThrown);
}
});
}
非常简单,populateEvents.php
完成了大部分工作.
Pretty simple, populateEvents.php
does most of the work.
现在,当我想添加事件时,我调用以下函数:
Now, when I want to add an event, I call the following function:
function createEvent(u_id) {
var title = $("#titleInput").val();
var type = $("#typeSelect").val();
var location = $("#locationInput").val();
var date = $("#inputDate").val();
var time = $("#inputTime").val();
var allday = $('#allDaySelect:checked').val() !== undefined ? '1' : '0';
var duedate = type == 'todo' ? date : '';
var notes = $("#inputNotes").val();
var output = 'u_id=' + u_id + '&title=' + title + '&type=' + type + '&location=' + location + '&date=' + date + '&time=' + time + '&allday=' + allday + '&duedate=' + duedate + '¬es=' + notes;
$.ajax({
type: "GET",
url: "addEvent.php",
data: output,
success: function(data, textStatus, XMLHttpRequest) {
if (data == '') {
toggleNewEvent();
populateEvents();
} else {
// extract form problems from formProblems, in form: title=empty&date=empty&...
var errorArray = data.split("&");
var fields = '';
$.each(errorArray, function(index, value) {
var field;
var smallArray = value.split("=");
field = smallArray[0];
fields = fields + field + ', ';
});
alert('You must fill in the following fields: ' + fields);
}
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Status: " + textStatus + ". Error thrown: " + errorThrown);
}
});
}
稍微复杂一点,但是我敢肯定您的要旨.发送到addEvent.php
的值,解析返回的字符串等,并重新填充事件.现在,我只是重组了整个PHP端,以实现eventlist
类和事件类.以前,addevent.php
文件完成了所有实际的后端工作,但是现在,该文件(经过一些验证)可以归结为:
A bit more complicated, but I'm sure you get the gist. Values sent to addEvent.php
, return string parsed, etc, events repopulated. Now, I just restructured the whole PHP side, to implement an eventlist
class and an event class. Previously, the addevent.php
file did all the actual backend work, but now, that file (after some validation) boils down to this:
if ($return == '') {
/*If return is empty, everything is fine, carry on.*/
$eventlist->addEvent($u_id, $title, $type, $date, $time, $allday, $location, $dueby, $notes);
} else {
/*Throw an error*/
echo ($return);
}
但是,结果是,此文件在将请求发送到eventlist
对象以执行addEvent
方法时返回到JavaScript函数.然后,该函数尝试立即填充事件div,结果,它不起作用,因为addEvent
方法尚未完成.但是,当您刷新页面时,它可以正常工作,因此我知道它可以正确插入它们,它只需要等待第二个PHP文件完成即可.
As a result however, this file returns to the JavaScript function when it's sent that request to the eventlist
object to execute the addEvent
method. The function then tries to populate the event div immediately, and as a result, it doesn't work, because the addEvent
method hasn't finished. When you refresh the page, however, it works fine, so I know it is inserting them correctly, it just needs to wait for that second PHP file to finish.
推荐答案
为什么不将$eventlist->addEvent($u_id, $title, $type.......
部分返回给脚本,echo $eventlist->addEvent($u_id, $title, $type.......
.
Why not return the $eventlist->addEvent($u_id, $title, $type.......
part to the script,echo $eventlist->addEvent($u_id, $title, $type.......
.
然后在您的addEvent
方法中返回一些信息,即事件的ID.
Then in your addEvent
method return something, i.e. the ID of the event.
然后而不是检查页面是否为空白,而是要检查ID ( if (!isNaN(data))
isNotANumber ),因此您知道您已完成插入事件的操作,因为只有拥有ID的页面才能返回页面.
Then instead of checking the page is blank, you are checking for an ID ( if (!isNaN(data))
isNotANumber ), thus you know you have finished inserting your event as your page wouldn't return until it has the ID.
这篇关于等待外部方法完成后再完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!