Hi guys.
I've been stuck with this headache for 3 days now.
I've tried different solutions, but I run into the same problem.
I can get the autocomplete to work when using local data.
But once I try to retrieve data from databae, nothing happends.
I've tested the DB connection and it works. I'm able to retrieve data and list this.
Prototype & Scriptaculous
I've downloaded the files from this example.
I can get this working with local data, but not with when I try conecting to DB.
This is the function in the index.php file:
document.observe('dom:loaded', function() {
new Ajax.Autocompleter('state_input', 'state_suggestions','auto2.php',{tokens:[',']});
});
<input type="text" name="state_input" id="state_input" size="30" />
<div id="state_suggestions"><!-- contains the suggestions --></div>
This is the code from the auto2.php file:
$suggestions //This array is processed and contains the DB result.
//Will store all the input values that matches with the database suggestions.
$matched = array();
// Foreach statement, goes through each value of the array specified individually.
foreach ($suggestions as $suggestion) {
if (stripos($suggestion, $value) !== FALSE) {
$match = preg_replace('/' .$value. '/i',"<strong>$0</strong>", $suggestion, 1);
$matched[] = "<li>$match</li>";
}
}
//Print all the suggestions. This is returned back to Ajax.Autocompleter.
echo "<ul>".join("", $matched)."</ul>";
- How can I test that the file auto2.php actually gets called?
- Why isn't the data ouputted?
Ajax Auto Suggest v.2
The other solution I've tried, is Ajax Auto Suggest from brandspankingnew.net.
This to I get working in "static"# mode.
This is part ofthe code that does tha Ajax call:
<form method="get" action="" class="asholder">
<small style="float:right">Hidden ID Field: <input type="text" id="testid" value="" style="font-size: 10px; width: 20px;" disabled="disabled" /></small>
<label for="testinput">Person</label>
<input style="width: 200px" type="text" id="testinput" value="" />
<input type="submit" value="submit" />
</form>
<script type="text/javascript">
var options = {
script:"test2.php?json=true&limit=6&",
varname:"input",
json:true,
shownoresults:false,
maxresults:6,
callback: function (obj) { document.getElementById('testid').value = obj.id; }
};
var as_json = new bsn.AutoSuggest('testinput', options);
var options_xml = {
script: function (input) { return "test2.php?input="+input+"&testid="+document.getElementById('testid').value; },
varname:"input"
};
var as_xml = new bsn.AutoSuggest('testinput_xml', options_xml);
</script>
The code in test2.php looks like this:
<?php
include_once('dal.php');
### Get input string and make sure it does not contain any harmfull characters
$tmp = strip_tags(strtolower( $_GET['input'] ));
$input = preg_replace('/&[^;]+;/','',$tmp);
### Get the number of characters that must be entered before a search will be done (mine 1 because it starts on 0)
$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;
$sl = new storeLocator();
$result = $sl->getSearchResultForLabels($input);
while ($row = mysql_fetch_array($result))
{
$display.=''.$row['label'].'';
}
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header ("Pragma: no-cache"); // HTTP/1.0
header("Content-Type: text/xml");
echo "<"."?xml version=\"1.0\" ?><status>$display</status>";
?>
- Why doesn't this produce any ouput?
I really need to get his working. I'm really behind on my project 🙁
Any help appreciated!