duanjianxi8439 2012-01-19 20:28
浏览 45
已采纳

jquery将变量传递给php文件

acctually i am not familier much with jquery.. i got this jquery script this is passing variables to the file which is showing data in json format.. but here i'm unable to show that data..plz see this piece of code

$(document).ready(function() {
    var globalRequest = 0;
    $('#search').bind('keyup', function(event) {
        if (event.keyCode == 13) {
            searchAction();
        }
    });

    $('#search-link').bind('click', function(event) {
        searchAction();
    });

    var searchAction = function() {
        var value = $('#search').val();
        var cat = $('#category').val();
        var country = $('#country').val();
        var page = $('#page').val();

        var resultContainer = $('#results');

        if (value.length < 3 && globalRequest == 1) {
            return;
        }

        _gaq.push(['_trackEvent', 'Search', 'Execute', 'Page Search', value]);

        globalRequest = 1;

        $.ajax({
              url: "search.php",
              dataType: 'json',
              type: 'GET',
              data: "q="+value+"&category="+cat+"&country="+country+"&page="+page,
              success: function(data){
                globalRequest = 0;
                resultContainer.fadeOut('fast', function() {
                    resultContainer.html('');
                    console.log(data.length);

                    for (var x in data) {

                        if (!data[x].price)
                            data[x].price = 'kA';

                        if (!data[x].img)
                            data[x].img = 'assets/images/no.gif';

                        var html = '<div class="res-container">';
                        html += '<h2><a href="'+data[x].url+'" target="_blank">'+data[x].Title+'</a></h2>';
                        html += '<img src="'+data[x].img+'">';
                        html += '<h3>Price: '+data[x].price+'</h3>';
                        html += '</div>';

                        resultContainer.append(html);
                    }

                    resultContainer.fadeIn('fast');
                });

              }
            });
    };
});

in search.php data is in simple echo.. how to get data from search.php and show here.. sorry for bad english

  • 写回答

2条回答 默认 最新

  • duan111112 2012-01-19 20:32
    关注

    First,

    you shouldn't concatenate your parameters but use a hashmap:

            $.ajax({
              url: "search.php",
              dataType: 'json',
              type: 'GET',
              data: {
              q : value,
              category : cat,
              country : country,
              page : page }
    

    As your method is (type: 'GET'), just use the ($_GET[param] method) in the php file

    <?php
    $value = htmlentities($_GET['q']);
    $category = htmlentities($_GET['category ']);
    $country = htmlentities($_GET['country ']);
    

    In the js callback function, this is how you log the whole response ('something' is a tag) :

    success: function(data){
        var $xml = $(data);
        console.log($xml); // show the whole response
        console.log($xml.find('something')); // show a part of the response : <something>value</something>
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line