Natasha
Natasha

Reputation: 259

jquery passing variables to php file

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

Upvotes: 1

Views: 667

Answers (2)

Zac
Zac

Reputation: 1635

It is a bit hard to understand what your problem is but my guess is that you need to json encode the data before echoing it back in search.php.

simplified example......

eg.

<?php
$somevar = $_GET['a']
$anothervar = $_GET['b']

//Do whatever


$prepare = array('a'=>$result1,'b'=>$result2) //etc..
$json = json_encode($prepare);
echo $json;
exit();
?>

Then you can access the results in the javascript with:

success: function(data){
    var obj = $.parseJSON(data);
    alert(data.a);
    $("#some_element").html(data.b);
}

Upvotes: 2

vaugham
vaugham

Reputation: 1851

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>
});

Upvotes: 5

Related Questions