Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PHP rather than JSON as output in instant search script

I have a Google Instant style search script written in jQuery which I want to pull results from a PHP script. I know my script currently needs JSON as the output but I want it to output PHP generated HTML instead. How can I do this?

Here is my code:

$(document).ready(function(){
    $("#search").keyup(function(){
        var search=$(this).val();
        var keyword=encodeURIComponent(search);
        var yt_url='http://www.SITEURL.com/search.php?action=SEARCH&keyword='+keyword+;
        window.location.hash=keyword;

        $.ajax({
            type:"GET",
            url:yt_url,
            dataType:"jsonp",
            success:function(response){
                $("#result").html('');
                if(response.SearchResponse.Web.Results.length){
                    $.each(response.SearchResponse.Web.Results, function(i,data){
                        var title=data.Title;
                        var dis=data.Description;
                        var url=data.Url;
                        var final="<div class='webresult'><div class='title'><a href='"+url+"'>"+title+"</a></div><div class='desc'>"+dis+"</div><div class='url'>"+url+"</div></div>";
                        $("#result").append(final);
                    });
                }
            }
        });
    });
});
like image 746
Callum Whyte Avatar asked Jan 27 '26 22:01

Callum Whyte


1 Answers

Just use

dataType:"html",

In your $.ajax call. The result will be returned as plain text, so if you just want to display it you can

success:function(response){
   $("#result").html(response);
}
like image 116
cbrandolino Avatar answered Jan 30 '26 11:01

cbrandolino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!