Question

Im working on a drupal site that uses jQtouch (for iphone), and im trying to make a search that doesnt reload the site. I think the javascript/ajax part isnt working. Im having problems finding out if the data from the php file is actually comming through, at the moment only the js that empties the search-results is working. Apperently jQtouch has ajax built into all forms or something. Does anyone have any interesting comments that could help me? btw this is the first time im using ajax and javascript

pensumsearch.php:

    $search = $_GET["search-text"];
    $result = db_query("SELECT nid FROM {node} WHERE title LIKE '%s' AND type = 'product_collection'", $search.'%');
    $noder = array();
    while ($record = db_fetch_object($result)) {
        $noder[] = $record;
    }
    $matches = array();
    foreach($noder as $row) {
        $node = node_load($row->nid);
        $res = db_query("SELECT tid FROM {term_node} WHERE nid ='$node->nid'");
        $termlink = (db_fetch_object($res));
        $matches[] = $node->title;

    }
header('Content-Type: application/json');
print json_encode($matches);


?>

search.js :

(function($){

    $(function(){

            $("#form").submit(function(event, info) {
                    var text = $("input[id=search-text]", this);
                    text.blur();

                    var results = $("#search-results", this).empty();
                    results.append($("<li>", {
                            "class": "arrow4",
                            text: 'Resultater "' + text.val() + '"'
                    }));

                    $.get("pensumsearch.php", { value: text.val() },
                            function(data) {
                                    $('#search-results').html(data);
                                    alert("Data Loaded: " + data);
                            }
                    );
                    return false;
            });
    });

})(jQuery);

Was it helpful?

Solution

It looks like you are using a stand alone php script, which means that Drupal shouldn't be bootstrapped and you basically can't use any of Drupal functions, unless you already bootstrap Drupal without showing. If you do bootstrap Drupal, there's not much point in using a stand alone PHP script.

Another thing is that you should put the % for the like query directly in the sql instead:

WHERE title LIKE '%s%' ... $search

Another thing is that you are doing:

$('#search-results').html(data);

Which should work, but shouldn't be pretty since it's a json encoded string you are inserting. You might considder using $.getJSON which will convert the data into json format.

You might also consider debugging your php script, or use firebug to see if you get the expected output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top