I have pretty much copied and pasted from this Stackoverflow question/answer, and it doesn't seem to want to work on my page. I also tried creating a blank PHP page to try it on, and it still doesn't work.
Also the jQuery library that I included in the header was one off Google's library, and also jQuery's official one: http://code.jquery.com/jquery-latest.min.js.
It has to be something with the encoding or something because the code works fine in my browser on JSFiddle also:
http://jsfiddle.net/kn3Qu/
Here is the code I'm using personally:
<input id=dam type=text />
<div id=dam_return>
<a href=#>DLH Victoria</a>
<a href=#>DLH Sam</a>
<a href=#>Studly</a>
</div>
and the Javascript:
$('#dam_return a').click(function() {
var value = $(this).html();
var input = $('#dam');
input.val(value);
return false;
});
If anyone could help me out that would be awesome!
EDIT: I'm thinking my problem is because of a SQL query that happens everytime a key is pressed on the dam input.
Here is the EXACT SQL query that happens:
<div id=dam_return_search><a href=#>Maybe this one works? (but no, it doesn't)</a><br />
<?php
$connect = // blahblahaa
if (isset($_GET['dam_text'])) {
getSuggest($text);
}
function getSuggest($text) {
$dam = $_GET['dam_text'];
$sqlCommand = SELECT `name` FROM `alpacas1` WHERE `name` LIKE '%$dam%';
$query = mysql_query($sqlCommand);
$result_count = mysql_num_rows($query);
while ($row = mysql_fetch_assoc($query)) {
echo '<a href=#>'.$row['name'].'</a><br />';
}
}
?>
</div>
Here is the DAM input box and code associated with it:
<script type=text/javascript>
function suggest1() {
var dam_text = document.getElementById('dam').value;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject('MicrosoftXMLHTTP');
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById('dam_return').innerHTML = xmlhttp.responseText;
}
}
var target = 'dam_search.php?dam_text=' + dam_text;
xmlhttp.open('GET', target, true);
xmlhttp.send();
}
</script>
<script type=text/javascript>
$(document).ready(function(){
$(#dam_return_search a).click(function(){
var value = $(this).html();
var input = $('#dam');
input.val(value);
});
});
</script>
<tr>
<td id=label>Dam:</td>
<td id=option>
<input type=text name=dam id=dam value=<?php echo $dam; ?> onkeyup=suggest1();><br />
<div id=dam_return></div>
</td>
</tr>