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

报告相同问题?