2

我的script.js执行对get_all.php的请求,该请求具有三个要输出的变量 (echo $all_categories)$all_brands$all_filters. 现在我想在我的index.html的三个不同区域中操作这些。

我想放置$all_categories<div id="caregories">、和$all_brands中。<div id="vrands">$all_filtesr<div id="filters">

我怎样才能将它们分别放在每个div?我知道如何将所有这些放在一个中,<div>但不知道如何分别放置每个收到的变量。

索引.php

<?php // Startup ?>

<?php 

    define('APP_PATH', '../');
    define('SYS_PATH', '../system/');
    define('STYLES', 'assets/styles/');
    define('SCRIPTS', 'assets/scripts/');

    require_once SYS_PATH . 'initializers/all.php'; 

?>

<?php // Controller ?>

<?php



?>

<?php // Viewer ?>

<?php template('header'); ?>

<body>

<div id="static_menu">
    <ul>
        <li><a href="#categories">Categories</a></li>
        <li><a href="#brands">Brands</a></li>
        <li><a href="#filters">Filters</a></li>
        <li><a href="#social">Social</a></li>
    </ul>
</div>
<hr>
<div id="categories">
    <ul></ul>
</div>
<hr>
<div id="brands">
    <ul></ul>
</div>
<hr>
<div id="filters">
    <ul></ul>
</div>
<hr>
<div id="social">
    <ul></ul>
</div>
<hr>

</body>

<?php template('footer'); ?>

脚本.js

// DOM ready
$(function(){

    // get categories, brands, filters and social
    $.ajax({
        type: "POST",
        url: "get_all.php"
    }).done(function(data){
        // manipulate recieved in three different divs
    })

});

get_all.php

<?php // Startup ?>

<?php 

    define('APP_PATH', '../');
    define('SYS_PATH', '../system/');
    define('STYLES', 'assets/styles/');
    define('SCRIPTS', 'assets/scripts/');

    require_once SYS_PATH . 'initializers/all.php'; 

?>

<?php // Controller ?>

<?php

$result_arr = $category->select_all();
$all_categories = $html->list_anchor_loop($result_arr, 'category');
echo $all_categories

$result_arr = $brand->select_all();
$all_brands = $html->list_anchor_loop($result_arr, 'brand');
echo $all_brands;

$result_arr = $filter->select_all();
$all_filters = $html->list_anchor_loop($result_arr, 'filter');
echo $all_filters;
4

3 回答 3

7

让您的 PHP 脚本将结果作为 JSON 对象发送(请参阅 参考资料json_encode)。

在您的 JS 中,您将收到一个对象,比如说resp,它将具有三个属性resp.categoriesresp.filtersresp.brands

有关更多详细信息和特异性,请发布一些代码。

get_all.php

$result = array(
  'categories' => $all_categories,
  'brands' => $all_brands,
  'filters' => $all_filters
);

echo json_encode($result);

script.js

$.ajax({
    type: "POST",
    url: "get_all.php",
    dataType : 'json', //specify that data will be returned as JSON
}).done(function(data){
    // use data.categories, data.brands, data.filters here
    // manipulate recieved in three different divs
})
于 2012-11-11T07:34:05.300 回答
1

如果您的 ajax 调用通过JSON返回,则很容易将数据插入到您需要的 div 中:

PHP

<?php
$return = array(
    'categories'=>$all_categories,
    'vrands'=>$all_brands,
    'filters'=>$all_filters
    );
echo $_GET['jsoncallback'].'('.json_encode($return).')';?>

查询:

<script type="text/javascript">
$.getJSON("URL_TO_PHP?jsoncallback=?",{
    /*DATA TO SEND*/
    },function(response){
        $('#categories').html(response.categories);
        $('#vrands').html(response.vrands);
        $('#filters').html(response.filters);

});
</script>
于 2012-11-11T07:37:27.070 回答
0

您可以使用自动执行此操作的库,例如http://phery-php-ajax.net ,您可以直接从 PHP 传递 JSON 数据,同时仍按顺序操作 DOM。Phery 不适用于 URL,但可以使用函数回调,您可以从 PHP 本身内部分配给 DIV,例如您的情况:

<?php

Phery::instance()->set(array(
    'get_all' => function($data){
       /* globals, initialization, etc*/
       $r = new PheryResponse;
       $result_arr = $category->select_all();
       return 
         $r
         ->jquery('#categories')->html($html->list_anchor_loop($result_arr, 'category'))
         ->jquery('#filter')->html($html->list_anchor_loop($result_arr, 'filter'))
         ->jquery('#brand')->html($html->list_anchor_loop($result_arr, 'brand'));

    }
))->process();

get_all然后从您的 DOMReady调用该函数

$(function(){
  phery.remote('get_all');
});

完成,一切都为您完成,无需任何额外的客户端代码。

于 2012-11-12T02:50:31.973 回答