When a user clicks on a link within the navbar of my web application the link's name gets assigned to a javascript variable ('team'). The variable then is sent to php via AJAX "POST" to use it for a mySQL query to populate the dashboard with new data. I got everything running but the success function of the AJAX part.
When i try to grab the dashboard inputs for the html div update I fail to correctly grab the information needed which is sent as array in the callback.
According to the developer tool network debug i have the following json array as callback in the php part:
array(7) { ["team_id"]=> string(1) "2" ["name"]=> string(17) "Borussia Dortmund" ["logo"]=> string(56) "https://github.com/Phanti1893/dasocc/blob/master/165.png" ["founded"]=> string(4) "1909" ["venue_capacity"]=> string(5) "81365" ["squad_value"]=> string(3) "634" ["total_national_trophies"]=> string(2) "12" }
When i use the following success function, I just get an 'y' in the html container (which obviously is the 4th index figure within the callback in 'array'). Here i want to grab the single elements within the array to populate my dashboard at the frontend(.selectedClub is one example of many):
$('ul.subbar li a').on('click', function(e) { // Start function when user clicks on a team in the navbar
e.preventDefault(); // Stop loading new link?
var team = $(this).html(); // Assign clicked team name to variable
$.ajax({
method: "POST",
url: "includes/script.php",
data: {team: team},
data_type: "json",
success: function(data) {
$('.selectedClub').html(data[4]);
}
});
console.log(team); // check for console, remove later
});
this is my php script that makes the SQL query with the variable 'team':
<?php
include_once 'dbh.inc.php';
if (isset($_POST['team'])) {
$team = $_POST['team'];
$sql = "SELECT * from teams WHERE name = '$team';"; // task sent to server
$result = mysqli_query($conn, $sql); // returns object resource
$teamarray = $result->fetch_assoc(); // pass object to an array
$name = $teamarray ["name"];
$logo = $teamarray ["logo"];
$founded = $teamarray ["founded"];
$venue_capacity = $teamarray ["venue_capacity"];
$squad_value = $teamarray ["squad_value"];
$total_national_trophies = $teamarray ["total_national_trophies"];
var_dump($teamarray);
}
?>
EDIT: I guess the problem is that I don't get a JSON array in return but plain HTML/text due to chrome developer tools:
Connection: Keep-Alive
Content-Length: 341
Content-Type: text/html; charset=UTF-8
Date: Sun, 22 Sep 2019 09:11:57 GMT
Keep-Alive: timeout=5, max=97
Server: Apache/2.4.41 (Win64) OpenSSL/1.1.1c PHP/7.3.9
X-Powered-By: PHP/7.3.9