I don’t understand how to change this html from displaying a string to displaying relevant suggestions in a drop down format that when clicked will fill the input box. How do I do that?
Thanks, but can’t the scripts I’ve posted be modified. I understand them and need to use them to better understand how and why they need to be changed. That’s ok isn’t it?
If so, I was told to change this line: document.getElementById(“txtHint”).innerHTML=xmlhttp.responseText;
Couldn’t I just leave that as is and do all the formatting in the php script? I’m obviously new to JS though I’m way less challenged by php.
The above html is invalid because the <a> element does not have an attribute called value.
To get the text in the link copied to the input box, assign an onclick event handler to take the link’s text and assign it to the value property of the input box. This is done with javascript.
I’ve played around with this for awhile and I don’t understand how to work the onchange event handler into <select> and how that’s going to get what’s selected into the input box. Can you show me please?
let’s talk about it both ways. first however, here’s my current php file. It displays a wrapper div under the input field with anchors in it, but I don’t know how the get the anchor values into the input field when one is selected. How do i do that?
<?php
// Fill up array with names
$a[]="Bob";
$a[]="Brittany";
$a[]="Brian";
//echo var_dump($a) . '<br/>';
//get the q parameter from URL
$q=$_GET["q"];
//lookup all hints from array if length of q>0
$hint = '';
$html = '';
$html = "<div name='firstname' id='firstname'>";
if (strlen($q) > 0) {
foreach ($a as $key=>$value) {
if (strpos(strtolower($value), $q) !== false) {
//echo "$value\
";
$html .= "<a style=\\"text-decoration:none;\\" href=".$value." value=". $value . " >".$value."</a></br>";
//$hint .= $value . " <br />";
}
}
}
$html .= "</div>";
$hint = $html;
// 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 $response;
?>
That depends on whether you want the hints displayed with or without a page refresh.
If you want a page refresh then you can easily do it all in php and remove your ajax code. If you don’t want a page refresh then you will need to use AJAX, as you are currently doing, and make the changes I suggested earlier.
to create either links (<a> elements) or the option elements for a <select> using the names in xmlhttp.responseText
If you decide to use <a>'s then you will need to add an onclick event handler to each <a> to transfer the clicked name to the input box. If you decide to use a <select>, it will need an onchange event handler to transfer the clicked name to the input box.
This sounds like a homework exercise, so give it a go and if you get stuck post back with your updated code and someone will try to help.
HTML5 has something for this, but it’s not widely supported yet. You’d need some more robust JavaScript for that. Perhaps do a search for something like “jquery form dropdown suggestions”. (That gave some useful results.)
If they are in a database, then you could use AJAX to send the user input to a server side script which retrieves the relevant records for the user input and then returns them to the browser. The AJAX request then uses the returned records to populate a <select> with the returned records to be used as suggestions. The user then selects an option from the <select> which is then automatically inserted in the input text box.
Probably should skip the html5 for right now. I’m learning a lot of new stuff as is.
All the input possibilities are in a php array that’s selected, for relevant options, with a for loop. Please confirm that that I need to get the input box and the <select> tag into a <div>. If so, I’m unclear about the code that will place a selected option in the input box. How would I do that? Also, please confirm that placing the input box and the selectable options can be part of an acceptable solution.
I understand. Can we work on the div for the the links under the input field and then then a snippet to update the links that go into the link container?
I’ve never updated a page without page refresh so what I know is this: