Javascript 如何获取AJAX SQL回调(数组)中的特定元素以访问变量?

Javascript 如何获取AJAX SQL回调(数组)中的特定元素以访问变量?,javascript,php,mysql,ajax,Javascript,Php,Mysql,Ajax,当用户单击我的web应用程序导航栏中的链接时,该链接的名称被分配给javascript变量(“团队”)。然后,该变量通过AJAX“POST”发送到php,用于mySQL查询,用新数据填充仪表板。除了AJAX部分的成功功能外,我运行了所有的东西 当我尝试获取html div更新的仪表板输入时,我无法正确获取所需的信息,这些信息在回调中作为数组发送 根据开发人员工具network debug,我在php部分有以下json数组作为回调: array(7) { ["team_id"]=> stri

当用户单击我的web应用程序导航栏中的链接时,该链接的名称被分配给javascript变量(“团队”)。然后,该变量通过AJAX“POST”发送到php,用于mySQL查询,用新数据填充仪表板。除了AJAX部分的成功功能外,我运行了所有的东西

当我尝试获取html div更新的仪表板输入时,我无法正确获取所需的信息,这些信息在回调中作为数组发送

根据开发人员工具network debug,我在php部分有以下json数组作为回调:

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" }
当我使用下面的success函数时,我只在html容器中得到一个“y”(这显然是“array”中回调中的第四个索引数字)。在这里,我想获取阵列中的单个元素,以填充前端的仪表板(.selectedClub是许多示例中的一个):

这是我的php脚本,它使用变量“team”进行SQL查询:

<?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);

}
?>

这真的应该是一个评论,但我仍然在努力提高我的声誉

我不明白你为什么期望数据[4]是“y”


我建议您在控制台上记录返回的数据,以便准确地查看Ajax调用返回的内容。我不喜欢php,但您看到的返回值似乎是一个对象而不是数组,您可能希望访问
数据。name
而不是数组元素,但console会首先记录它,经过两天的调试,它现在工作如下,关键是 将SQL对象编码为JSON数组,并在javascript代码中解析该数组,以便能够正确获取数据

非常感谢您的投入,非常感谢

javascript/AJAX:

$('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(team) {

    team = $.parseJSON(team);
    console.log(team);
    $('.selectedClub').html(team.name);
    $('#founded').html(team.founded);
    $('#venue_capacity').html(team.venue_capacity);
    $('#squad_value').html(team.squad_value);
    $('#total_national_trophies').html(team.total_national_trophies);
    $('#teamlogo').attr({src: team.logo});


    }

  });

});

警告:您完全可以使用参数化的预处理语句,而不是手动生成查询。它们由或提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行。感谢您的强调,我将研究它,并尝试在这方面改进我的代码。但是你能不能给我一些关于所描述问题的提示?当我在javascript中输入console.log“data”时,我得到的内容与最初文章顶部显示的数组中的内容相同。我假设我必须首先操作回调数据类型才能获取元素?它似乎是纯html/文本?
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
$('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(team) {

    team = $.parseJSON(team);
    console.log(team);
    $('.selectedClub').html(team.name);
    $('#founded').html(team.founded);
    $('#venue_capacity').html(team.venue_capacity);
    $('#squad_value').html(team.squad_value);
    $('#total_national_trophies').html(team.total_national_trophies);
    $('#teamlogo').attr({src: team.logo});


    }

  });

});
<?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"];

echo json_encode($teamarray); // changes object into json format
//var_dump($teamarray);

}
?>