Link to home
Start Free TrialLog in
Avatar of Crazy Horse
Crazy HorseFlag for South Africa

asked on

Displaying text in text field when clicking on ajax search result

When a user types into a text field, results start showing from the database via AJAX. When they click on a result, I want it to show up in the text field. I have used this to show a static value so it seems to be the right idea but I don't know how to get the dynamic value from the database into the text field:

$('#result' + textfield_value).click(function() {
        $('#search' + textfield_value).val();

    });

Open in new window


Obviously I need to put something into val()

The php :

if(isset($_POST['search'])) {
	
	$search = "{$_POST['search']}%";
	$stmt = $link->prepare("SELECT `lost_time_desc` FROM `lost_time_reasons` WHERE `lost_time_desc` LIKE ?");
	$stmt->bind_param("s", $search);
	$stmt->execute();
	$result = $stmt->get_result();
	$numRows = $result->num_rows;
	if($numRows > 0) {
		while($row = $result->fetch_assoc()) {
			$lost_time_reason = sanitize($row['lost_time_desc']);
			echo "<li>" . $lost_time_reason. "</li>";
		}
	}
	
	$stmt->close();
}

Open in new window

Avatar of Rob
Rob
Flag of Australia image

Have you tried:

$('#search' + textfield_value).val($('#result' + textfield_value).text());
Avatar of Crazy Horse

ASKER

Hey Rob,

Thanks. I had not tried that but upon trying your code what happens is that if there are 2 search results and I click on one, it adds both to the text field instead of just the one I clicked on.
Can you please post the html that's returned from the php script.  ie what "text()" actually refers to.  Sounds like I need to drill down a bit further but I'm not sure what to aim at :)

eg for arguments sake if your php script returns:
<ul>
<li>result one</li>
<li>result two</li>
</ul>

and the click happens on the <ul> element then yes, you'll see both results
Also, when choosing an option, the list of options should disappear but it doesn't.

If I try this:

$('#result' + textfield_value).click(function() {
        $('#search' + textfield_value).val($('#result' + textfield_value).text());
		 $('#result' + textfield_value).html('');
	
    });

Open in new window


it removes the list of results but also removes whatever was in the text field too.
Not sure if this is what you are after?

<input id='search1' class='searchBox' name='search[]'type='text' autocomplete='off' onKeyUp='ajax_call("1")'><div style='display:none' id='loading1'><img src='functions/loading.gif'/></div><ul class='search-result' id='result1'></ul>
<input id='search2' class='searchBox' name='search[]'type='text' autocomplete='off' onKeyUp='ajax_call("2")'><div style='display:none' id='loading2'><img src='functions/loading.gif'/></div><ul class='search-result' id='result2'></ul>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks so much Rob. I have tried it out and what is happening now is that when I click on an item, it removes that item from the search results list and doesn't put anything into the text field.
Oh, hang on. Just saw your second post now. That seems to be working !:)

$('#result' + textfield_value).on('click', 'li', function(e) {
        $('#search' + textfield_value).val($(e.target).text());
	    $(e.target).siblings().remove();
    $(e.target).remove();
    });

Open in new window

Yes I was about to post a demo! :D

http://jsbin.com/lizomum/edit?html,js,output