I meet a problem the with a false url code .
for example:
"Allée des cassis" should be "All%C3%A9e%20des%20cassis%0A"
but my code produce "All%E9e%20des%20cassis"
then the decodeURIComponent javascript function product an URIerror :-(
May be you have a way to solve this problem.
Thanks
I use this code livesearch.php
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("livesearch.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0)
{
$hint="";
for($i=0; $i<($x->length); $i++)
{
$y=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
$massif=$x->item($i)->getElementsByTagName('title');//var massif
if ($y->item(0)->nodeType==1)
{
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
{
if ($hint=="")
{
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
else
{
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint=="")
{
$response="Oups! Aucune suggestion.";
}
else
{
$response=$hint;
}
//output the response
echo $response;
?>
and the xml file
<?xml version="1.0" encoding="UTF-8"?>
<pages>
<link>
<title>95.2</title>
<url>blocableau3x.html?95.2</url>
</link>
....
....
<link>
<title>Allée des cassis</title>
<url>blocableau3x.html?Allée des cassis</url>
</link>
<link>
<title>Apremont</title>
<url>blocableau3x.html?Apremont</url>
</link>
</pages>
Related
https://www.w3schools.com/php/php_ajax_livesearch.asp
I am trying to modify the code in the tutorial for an ajax live search, to include a new field called keyword. I want the search to also search the keyword field in order to improve user experience.
I have already modified the link.xml fine to include the newly added keyword tag.
<link>
<title> title </title>
<keyword> keywords here </keyword>
<url>https://path to url</url>
</link>
I am kind of stuck in the area below, trying to modify the conditional statement.
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
$hint="";
for($i=0; $i<($x->length); $i++) {
$y=$x->item($i)->getElementsByTagName('title');
$k=$x->item($i)->getElementsByTagName('keyword'); // New field added
$z=$x->item($i)->getElementsByTagName('url');
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
} else {
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no suggestion";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
I think you can change the condition
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
to
$checkTitle = stristr($y->item(0)->childNodes->item(0)->nodeValue,$q);
$checkKeyword = stristr($k->item(0)->childNodes->item(0)->nodeValue,$q);
if ($checkTitle || $checkKeyword) {
//
}
trying to add a Ajax search function to a xml file, and found this exemple :
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("my_xml_file.xml");
$x=$xmlDoc->getElementsByTagName('links');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
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) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
} else {
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no suggestion";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
This works perfect but the xml I have to search is a bit different and I don't know how to setup the function according to my needs.
This is how is formatted my xml :
<data name="my_name"><![CDATA[Data content]]></data>
I changed
`$x=$xmlDoc->getElementsByTagName('links'); to $x=$xmlDoc->getElementsByTagName('data');`
But how to change
$y=$x->item($i)->getElementsByTagName('title');
to have
$y=$x->item($i)-> name; and $z=$x->item($i)->Data content without CDATA ?;
Thanx a lot for your help !!!
add this in your loop:
$attr = $a->item($i)->getAttribute('name');
if($attr==""){
continue;
}
//take care of the $attr
echo $attr;
I am using AJAX live search to generate user-profile-specific links. It works well, I always end up at the profile I want to, but there ist an issue.
Let's do this for user 1 (username = testuser; user_id = 1; blogname = testblog). If I search for "test", both links will be displayed, the link to testuser's profile, and the link to testuser's blog. The strange thing now is, the links work as if they would look like this:
profile.php?user=1&page=profile
profile.php?user=1&page=blog
but the actual links look like this:
profile.php?user=%20+%201%20+%20&page=profile
profile.php?user=%20+%201%20+%20&page=blog
Since I end up on the page I want to, you could say it doesn't matter, but it does, because I need the $GET_['user'] values always to be real numbers, not that kind of stuff I'm dealing with, here.
I hope there is some easy way to fix this. Like nodeValue->string or something. I need to change the nodeValue in this part of the code I think: $z->item(0)->childNodes->item(0)->nodeValue
This is the code I'm using:
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("../xml/accounts.xml");
$x=$xmlDoc->getElementsByTagName('account');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
$hint="";
for($i=0; $i<($x->length); $i++) {
$y=$x->item($i)->getElementsByTagName('username');
$b=$x->item($i)->getElementsByTagName('blogname');
$c=$x->item($i)->getElementsByTagName('companyname');
$z=$x->item($i)->getElementsByTagName('user_id');
//search for usernames
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint= "<a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
} else {
$hint= $hint . "<br /><a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
}
}
}
//search for blognames
if ($b->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($b->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint= "<a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=blog' >" .
$b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";
} else {
$hint= $hint . "<br /><a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=blog' >" .
$b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no QuickResults, hit enter";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
Inside my XMLfile the structure looks like this, if it helps:
<account>
<username>testuser</username>
<user_id>1</user_id>
<blogname>testblog</blogname>
</account>
The problem you are getting arises from the fact that your code adds spaces and a plus sign to the resulting link. And spaces are automatically encoded as %20. The solution would be to remove them from the code like this:
$hint= "<a href='profile.php?user=" .
$z->item(0)->childNodes->item(0)->nodeValue .
"&page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
This change would need to be done in all four occurences.
I am using AJAX live search to generate user-profile-specific links. It works well, I always end up at the profile I want to, but there ist an issue.
Let's do this for user 1 (username = testuser; user_id = 1; blogname = testblog). If I search for "test", both links will be displayed, the link to testuser's profile, and the link to testuser's blog. The strange thing now is, the links work as if they would look like this:
profile.php?user=1&page=profile
profile.php?user=1&page=blog
but the actual links look like this:
profile.php?user=%20+%201%20+%20&page=profile
profile.php?user=%20+%201%20+%20&page=blog
Since I end up on the page I want to, you could say it doesn't matter, but it does, because I need the $GET_['user'] values always to be real numbers, not that kind of stuff I'm dealing with, here.
I hope there is some easy way to fix this. Like nodeValue->string or something. I need to change the nodeValue in this part of the code I think: $z->item(0)->childNodes->item(0)->nodeValue
This is the code I'm using:
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("../xml/accounts.xml");
$x=$xmlDoc->getElementsByTagName('account');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0) {
$hint="";
for($i=0; $i<($x->length); $i++) {
$y=$x->item($i)->getElementsByTagName('username');
$b=$x->item($i)->getElementsByTagName('blogname');
$c=$x->item($i)->getElementsByTagName('companyname');
$z=$x->item($i)->getElementsByTagName('user_id');
//search for usernames
if ($y->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint= "<a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
} else {
$hint= $hint . "<br /><a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
}
}
}
//search for blognames
if ($b->item(0)->nodeType==1) {
//find a link matching the search text
if (stristr($b->item(0)->childNodes->item(0)->nodeValue,$q)) {
if ($hint=="") {
$hint= "<a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=blog' >" .
$b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";
} else {
$hint= $hint . "<br /><a href='profile.php?user= + " .
$z->item(0)->childNodes->item(0)->nodeValue .
" + &page=blog' >" .
$b->item(0)->childNodes->item(0)->nodeValue . "</a><span> (blog)</span>";
}
}
}
// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
$response="no QuickResults, hit enter";
} else {
$response=$hint;
}
//output the response
echo $response;
?>
Inside my XMLfile the structure looks like this, if it helps:
<account>
<username>testuser</username>
<user_id>1</user_id>
<blogname>testblog</blogname>
</account>
The problem you are getting arises from the fact that your code adds spaces and a plus sign to the resulting link. And spaces are automatically encoded as %20. The solution would be to remove them from the code like this:
$hint= "<a href='profile.php?user=" .
$z->item(0)->childNodes->item(0)->nodeValue .
"&page=profile' >" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a><span> (profile)</span>";
This change would need to be done in all four occurences.
How do i stop my search bar from opening a new tab? I dont see whats making it do that? Heres my php code. I've been having a couple issues with this search bar recently...
Heres my code (It is very very long...):
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
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)
{
//find a link matching the search text
if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
{
if ($hint=="")
{
$hint="<a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
else
{
$hint=$hint . "<br /><a href='" .
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
$y->item(0)->childNodes->item(0)->nodeValue . "</a>";
}
}
}
}
}
// Set output to "No suggestions" if no hint were found
// or to the correct values
if ($hint=="")
{
$response="No suggestion";
}
else
{
$response=$hint;
}
//output the response
echo $response;
?>
you need to remove remove target='_blank'
Replace this
$z->item(0)->childNodes->item(0)->nodeValue .
"' target='_blank'>" .
by this
$z->item(0)->childNodes->item(0)->nodeValue .
"'>" .
tag target="_blank" make opening link in new window