0

Hiya Guys,

Been working on the w3schools livesearch example but you can only select the option via a link. I'm trying to do it so i can submit a value to return database results.
The external php code is below if anyone wants a look:
I got it returning the results from the xml file but the dropdown box appears under the text box so that value will not transfer into it.
I don't know if it's actually doable the way i'm trying because the results are returned into a <div id="txtupdate"></div> in the html page. Is there a way i can call the data into the html page without a predefined input box getting in the way?

cheers,


<?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');
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="<option value='" . $y->item(0)->childNodes->item(0)->nodeValue. "'>" . $y->item(0)->childNodes->item(0)->nodeValue . "</option>" . "<br />";
}
else
{
$hint=$hint . "<option value='" . $y->item(0)->childNodes->item(0)->nodeValue. "'>" . $y->item(0)->childNodes->item(0)->nodeValue . "</option>" . "<br />";
}
}
}
}
}

// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}

//output the response
echo "<select name='Classification'>";
echo $response;
echo "</select>";
?>

2
Contributors
7
Replies
10
Views
7 Years
Discussion Span
Last Post by baseballfury
0

Wanted to help you there, but your code is not very nice to read. Edit your post and put the code in code tags, you have to remember that! :)

0

thats how my external php file looks, i've copied it directly from there.
All i want to know is can i code the php to print the select tag directly into the dropdown box or will it always appear under it and in which case i have to redesign the way it is delivered into the html page.
At the moment the content is updated into the html via a div tag (as per the w3school example) where as ideally it would be delivered with an input tag.

cheers,

tom

0

I wanted to wrap your code in code tags so it's many times easier for me to read and understand.

Edited by happygeek: fixed formatting

0

You want to print some text, without that having to be to an <input> box? Well make a <div> object with an id and set its innerHTML, look:

<script type="text/javascript">
function postResult(myResult)
{
    document.getElementById('output').innerHTML = myResult;
}
</script>
<div id="output">Please search</div>

innerHTML is at the start "Please search" but then with the sample function "Please search" will be replaced with the content of 'myResult'.

Edited by Excizted: n/a

0

sorry dude i wasnt being very clear i know....i just wanted to get the livesearch function working. So i thought i would try and create an dropdown box that also gave the user suggested words but thinking about it thats probably not possible as dropdown boxes are readonly anyway i think....
What it is the w3schools example uses url links which means i had to get rid of the links but that meant the suggestion would come up but the user couldnt select them.

I just want an autocomplete box that works basically but i've gone a bit off track trying different methods

0

Oh well sorry then but I have no clue how to create that slide-down thingy.
It's probably not a good place to look for that in the PHP forums, maybe try some HTML/CSS forums?

0

thanks for giving it a go,
I just need to know what tag to use for each suggested word that will allow the user to select it and it generate inside the input box.

will take your advice about the html forum was hoping i'd be able to tag it inside the php file with echo and all that good stuff.

cheers

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.