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条)

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写