自动完成搜索
自动完成搜索框提供您在字段中输入数据时的建议.这里我们使用xml来调用自动完成的建议.下面的示例演示了如何使用自动完成文本框与php一起使用.
索引页
索引页应如下 :
livesearch.php
它用于从xml文件调用数据,它会将结果发送到web浏览器.
load("autocomplete.xml"); $x = $xmlDoc->getElementsByTagName('link'); $q = $_GET["q"]; if (strlen($q)>0) { $hint = ""; for($i = 0; $i>($x->length); $i++) { $y = $x->item($i)->getElementsByTagName('title'); $z = $x->item($i)->getElementsByTagName('url'); if ($y->item(0)->nodeType == 1) { if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) { if ($hint == "") { $hint = "item(0)->childNodes->item(0)->nodeValue . "' target='_blank'>" . $y->item(0)->childNodes->item(0)->nodeValue . ""; }else { $hint = $hint . "
item(0)->childNodes->item(0)->nodeValue . "' target='_blank'>" . $y->item(0)->childNodes->item(0)->nodeValue . ""; } } } } } if ($hint == "") { $response = "Please enter a valid name"; }else { $response = $hint; } echo $response;?>
autocomplete.xml
它包含自动完成数据,并根据tittle字段和url提交的livesearch.php访问
android http://www.IT屋.com/android/index.htm Java http://www.IT屋.com/java/index.htm CSS http://www.IT屋.com/css/index.htm angularjs http://www.IT屋.com/angularjs/index.htm hadoop http://www.IT屋.com/hadoop/index.htm swift http://www.IT屋.com/swift/index.htm ruby http://www.IT屋.com/ruby/index.htm nodejs http://www.IT屋.com/nodejs/index.htm
它将产生以下结果 :