Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
159
rated 0 times [  161] [ 2]  / answers: 1 / hits: 23470  / 11 Years ago, sun, october 13, 2013, 12:00:00

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>

More From » jquery

 Answers
2

Instead of having Javascript in the initial file change the input box dam I changed it so that the dam_search.php file changed it onclick.



Here is the code I had:



<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 what I changed it to:



<?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=# onclick=document.getElementById('dam').value=''.$row['name'].'';>'.$row['name'].'</a><br />';
}

}

?>
</div>

[#75026] Friday, October 11, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yaquelina

Total Points: 517
Total Questions: 101
Total Answers: 96

Location: Egypt
Member since Tue, Jul 6, 2021
3 Years ago
yaquelina questions
;