javascript - 使用 JSON 从 JavaScript/jQuery 中的 PHP 获取数组,然后对其进行操作?

标签 javascript php jquery json

所以我遇到了一个相当棘手的问题。我已经很接近了——非常接近了! - 解决它但还没有完全解决。我见过很多例子,但没有一个完全符合我的要求。

在 php 页面 myreqpdo.php 上,我从 MySQL 表中恢复一些数据行并将它们存储在二维数组中。在我的页面 index.php 上,我需要通过 JavaScript 访问该数组并稍微使用一下,然后再将其发送到我需要填充的内容。

这是 myreqpdo.php:

$locWanted = $_POST['searchTerm'];
echo json_encode(getVillesCPs($locWanted));

function getVillesCPs ($searchTerm) {
    // code to access DB here
    return $resultats;
}

我找到了this example使用 jQuery,但它是针对一个实例,在该实例中,人们只想将列表直接插入代码中,而不进行任何进一步的操作:

<html>
<head>
        <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script>
</head>
<body>

        <!-- this UL will be populated with the data from the php array -->
        <ul></ul>

        <script type='text/javascript'>
        $(document).ready(function(){
                /* call the php that has the php array which is json_encoded */
                $.getJSON('json_encoded_array.php', function(data) {
                        /* data will hold the php array as a javascript object */
                        $.each(data, function(key, val) {
                                $('ul').append('<li id="' + key + '">' + val.first_name + ' ' + val.last_name + ' ' + val.email + ' ' + val.age + '</li>');
                        });
                });

        });
        </script>

</body>
</html>

这对我不起作用,因为这个函数是根据搜索参数触发的。我需要类似以下伪代码的内容:

<script>
    var myArray = {code to recover PHP array};
    // manipulations for myArray here, using data to fill values of items on page
</script>

我一直认为答案就在我眼皮底下,但我真的没有看到我想要的例子!有什么想法吗?

最佳答案

您不需要 jQuery 在 JavaScript 代码中插入来自 PHP 的数组,只需直接在需要的地方插入即可:

<script>
    var myArray = <?php echo json_encode(getVillesCPs($locWanted)); ?>;
    // manipulations for myArray here, using data to fill values of items on page
</script>

请注意,您必须在此代码之前为 PHP 代码中的 $locWanted var 设置一个值,否则将使用空参数调用 getVillesCPs

<小时/> 编辑:由于您的 getVillesCPs 是在另一个 PHP 文件中定义的,因此在调用它之前将其包含在您的主页中(假设您的主页是一个 PHP 文件):

<?php 
//including the PHP file containing the function
include 'myreqpdo.php';
//setting a value for $locWanted, used then in the rest of the page
$locWanted = 'Paris';
?>
<script>
    var myArray = <?php echo json_encode(getVillesCPs($locWanted)); ?>;
    // manipulations for myArray here, using data to fill values of items on page
</script>

并删除 myreqpdo.php 中的前 2 行代码,您不想在调用函数之前echo任何内容。

关于javascript - 使用 JSON 从 JavaScript/jQuery 中的 PHP 获取数组,然后对其进行操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27755665/

相关文章:

javascript - 使用jQuery修改样式时,能否捕获到HTTP错误?

javascript - 嵌入html中的Cordova 5.0 javascript对android设备没有影响

php - Android 位图图像未使用 PHP API 保存到 MySQL

php - 如何模拟 "MYSQL has gone away"错误?

javascript - 如何重用克隆的元素?

javascript - 将 FullCalendar 从版本 1 升级到最新版本 2.3.2 Moment _ordinalParse 未定义

javascript - 如何解决javascript模数运算符不准确的问题?

javascript - 使用原生 JS 将 HTML 元素替换为字符串内容

javascript - PHP - 脚本执行时无法检查 session

javascript - PHP - 通知 : Undefined index: and not displaying data