/    Sign up×
Community /Pin to ProfileBookmark

AJAX: Ajavscript variable to query SQL via php

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 = $_
    POST[‘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>`

    to post a comment
    PHP

    1 Comments(s)

    Copy linkTweet thisAlerts:
    @daveyerwinSep 21.2019 — @Phanti#1608950

    ``<i>
    </i>$('ul.subbar li a').on('click', function(e) {
    e.preventDefault();
    var team = $(this).html();
    $.ajax({
    method: "POST",
    url: "includes/script.php",
    data: {team: team}
    });
    });<i>
    </i>
    ``
    ×

    Success!

    Help @Phanti spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.24,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...