I have received help here und stackoverflow making an invite function. In a AJAX based search box, it is possible to invite teams. When searching for a team, finding it and then selecting it - the team will be stored in an array. However, I need the data from the array in PHP.
My question: can I transfer the javascript array to PHP using AJAX? The code can be seen below here. The AJAX part is made using 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;
}
The teams are stored in TeamList().
Thanks in advance.