【问题标题】:PHP Query Array variable to JQueryPHP查询数组变量到JQuery
【发布时间】:2013-12-01 01:37:17
【问题描述】:

我正在学习将通过 PHP 从 MySQL 检索到的数据传递给使用 jQuery 的 HTML 表单。

php 文件返回两个数组变量中的所有数据:$resultsa$resultsb

请随时纠正我的方法 - 我正在使用 .getjson 查询(但失败)来收集数据并将其存储在 JavaScript 变量中。

// retrievedata.php
<?php
    include('/include/connection.php');

    if (isset($getid))
    {
        $getid = $_GET['getid'];
    }
    $resultsa = array();
    $resultsb = array();
    $result = mysql_query("SELECT * FROM FB_FoodBrew");

    while($row = mysql_fetch_array($result))
    {
        $resultsa[] = $row['Food_Name'];
        $resultsb[] = $row['Drink_Varietal'];
    }

    echo json_encode(array_values($resultsa));
    echo json_encode(array_values($resultsb));

javascript 粘贴在此处的 HTML 页面部分中:

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
    <script src="assets/flex-slider/jquery.flexslider-min.js"></script>
    <script src="js/main.js"></script>
    <script>
        $.getJSON('page.php', function(data) {
            // assuming data is an array
            Array.forEach(function(item) {
                console.log(item);
            });
        });
    </script>
</head>

不幸的是,我在 javascript 方面的经验非常有限,而且我很难确定该脚本应该放在哪里、应该使用哪些变量等等。

【问题讨论】:

标签: javascript php jquery getjson json


【解决方案1】:

你是说:

$.getJSON('page.php', function(data)

但是页面没有命名为page.php,它是retrievedata.php。 所以它正在寻找 page.php 的输出但找不到任何东西!

【讨论】:

  • 我希望!这只是代表我正在尝试做的通用代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-25
  • 2014-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多