I'm trying to call a PHP function using AJAX. I've checked stack overflow for solutions (which it showed in masses) but it still doesn't seem to work for me. Note that the PHP code and JavaScript / jQuery / AJAX code are all in the same file.
Below is the code I wrote to try and get the desired results, the two PHP files included have nothing to do with any of the PHP functions I need to call so I did not post those.
(Note: The ExecuteAll() function gets called, but nothing happens after/in the AJAX code)
<input type="button" value="Show all client data" id="all">
<?
include('inc/php/stringgenerator.php');
include('inc/php/client.php');
$generator = new StringGenerator;
$clients = array();
for($i = 0; $i < 250; $i++){
$clients[$i] = new Client;
$clients[$i]->SetId($i);
$clients[$i]->SetAddress($generator->GetGeneratedString(rand(12, 24)));
$clients[$i]->SetNotes("Notes for client " . $i);
$scores = array();
for($j = 0; $j < 25; $j++)$scores[$j] = rand(0, 10);
$clients[$i]->SetScores($scores);
}
echo "Clients generated";
?>
<?
switch($_POST["functionname"]){
case 'DisplayAllClientData':
DisplayAllClientData();
break;
};
function DisplayAllClientData(){
for($i = 0; $i < count($clients); $i++){
$clients[$i]->PrintClientData();
echo "<hr>";
}
}
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$("#all").click(function(){ ExecuteAll(); });
});
function ExecuteAll(){
$.ajax({
type: "POST",
url: 'index.php',
data: {functionname: 'DisplayAllClientData'},
succes:function(){
alert("Success!");
}
});
}
</script>