user1178177
user1178177

Reputation: 27

Autocomplete with PHP

Its my html code :

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jqueryui.js"></script>
    <script>
        $( "#tags" ).autocomplete({
        url: 'Ajax.php?txt='        
        });
    </script>
</head>
<body>
<div class="demo">
<div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags" name="txt" />
</div>
</div>
</body>
</html>

and here is my Ajax.php

<? 
    $val = $_REQUEST["txt"];
    if($val == "251") { echo "its WoooW";}
    else 
    echo "Nothing found";
?>

But it's not working for autocomplete. What is my mistake?

Upvotes: 1

Views: 786

Answers (2)

Lawrence Cherone
Lawrence Cherone

Reputation: 46650

You must wrap your jquery into a function like this:

<script>
    $(function() {
        $( "#tags" ).autocomplete({
            source: "Ajax.php",
            minLength: 2
        });
    });
</script>

And you must return json_encoded content from Ajax.php (Example) It States within the documentation:

When a String is used (eg not a set array), the Autocomplete plugin expects that string to point to a URL resource that will return JSON data. It can be on the same host or on a different one (must provide JSONP). The request parameter "term" gets added to that URL.

<?php
$val = $_REQUEST["term"];

if($val == "251") { 
    $return="its WoooW 251";
}elseif($val == "123"){
    $return="its WoooW 123";
}else{
    $return="Nothing Found";
}

echo json_encode($return);
?>

Upvotes: 0

Rene Pot
Rene Pot

Reputation: 24815

You should've checked the documentation. The URL property isn't available, but it's the source property:

docs: http://jqueryui.com/demos/autocomplete/#option-source

Example

        $( "#tags" ).autocomplete({
            source: 'Ajax.php'
        });

The query will be added according to the link you provided

Upvotes: 1

Related Questions