Hey there,
after spending hours n hours of search and not getting a solution I hope someone in here can help me. I am currently building a dashboard for soccer teams. I have an index.php file along with separated javascript and php (for the SQL part) files. The user can select different soccer teams in the navbar (one at a time). According to his selection the dashboard updates the values (like squad value, total trophies, etc.) after queried the new data from the SQL database. Now my main problem is to pass the javascript variable ‘team’ (onclick assigned after the user selects a team) to the php part to use it for a SQL query.
In a nutshell:
– The user clicks a team name in the navbar– The teamname gets assigned to a javascript variable ‘team’– The js variable gets passed to php via AJAX $_POST (this doesn’t work)– The php part uses the passed variable to make a SQL query– The dashboard gets updated with the data from the SQL query
When i debug, the variable ‘team’ contains the correct clicked team name as string. However, var_dump($_POST); in php returns array(0), where the problem begins. It seems that the teamname isn’t passed to the php script and thus php can’t query the data from SQL. Please find the code snippets below:
I hope I described the issue fair enough. Feel free to drop some questions if not
Thank you.
script.php:
`<?php include_once ‘dbh.inc.php’;
if (isset($_POST[‘team’])) {
$team = $
echo $team;
} else {
echo ‘failed’;}
$sql = “SELECT * from teams WHERE name = ‘team’;”; // task sent to server
$result = mysqli_query($conn, $sql); // returns object resource
$resultCheck = mysqli_num_rows($result); // check how many rows/results are returned
$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”];?>`
dashboard.js:
` $(‘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},});
index.php snip:
` <div class=”topRow”>
<div class=”team”>
<div class=”teamLogo”>
<img class=”teamLogo” src=”” alt=”Manchester United”>
</div>
<div class=”selectedClub”><?php echo $name?></div>
</div>
</div>