我在这里收到了帮助,stackoverflow 制作了邀请功能。在基于 AJAX 的搜索框中,可以邀请团队。搜索团队时,找到它然后选择它 - 团队将存储在一个数组中。但是,我需要 PHP 数组中的数据。
我的问题:我可以使用 AJAX 将 javascript 数组传输到 PHP 吗?代码可以在下面看到。AJAX 部分是使用 jQuery 制作的。
//AJAX based search
var $j = jQuery.noConflict();
$j(document).ready(function(){
$j("#search_results").slideUp();
$j("#button_find").click(function(event){
event.preventDefault();
search_ajax_way();
});
$j("#search_query").keyup(function(event){
event.preventDefault();
search_ajax_way();
});
});
function search_ajax_way(){
$j("#search_results").show();
var search_this=$j("#search_query").val();
$j.post("search_team.php", {searchit : search_this}, function(data){
$j("#display_results").html(data);
})
}
// <----- AJAX search ends ------>
//Add-remove teams
//Functional object for team
var Team = function (id, name) {
this.name = name;
this.id = id;
}
//Array which will contain teams
var TeamList = [];
//Checking if the team is already added
function containsTeam(id) {
for (var i = 0; i < TeamList.length; i++) {
if (TeamList[i].id == id) {
return true;
}
}
return false;
}
// Removed team by onclick-event
function removeTeam(id) {
for (var i = 0; i < TeamList.length; i++) {
if (TeamList[i].id == id) {
TeamList.splice(i, 1);
document.getElementById('teams').removeChild(document.getElementById('ta'+id));
}
}
}
function addTeam(tid) {
// Grab the input value
var teamName = document.getElementById(tid).innerHTML;
var teamID = document.getElementById(tid).id;
// If empty value
if(!teamName || !teamID) {
alert('An error occured.');
} else {
if(containsTeam(teamID) == false) {
TeamList.push(new Team(teamID, teamName));
// Create the teams with the value as innerHTML
var div = document.createElement('div');
div.className = 'team-to-invite';
div.setAttribute('onclick', 'removeTeam('+teamID+')');
div.onclick = function() { removeTeam(teamID) };
div.id = 'ta' + teamID;
div.innerHTML = teamName;
// Append it and attach the event (via onclick)
var teams = document.querySelector('#teams').getElementsByTagName('div');
document.querySelector('#teams').appendChild(div);
}
}
return false;
}
团队存储在 TeamList() 中。
提前致谢。