Jump to content

how to auto submit the auto suggested result


mahenda

Recommended Posts

//php code
<?php
include_once('con.php');
if(!empty($_GET['search'])) {
$search = $connect->prepare('SELECT * FROM posts WHERE title LIKE :search');
$search->bindValue(':search', '%'.$_GET['search'].'%', PDO::PARAM_STR);
$search->execute();
?>
<?php
while($row = $search->fetch()) {?>
<li class="result" onClick="searchValue('<?php echo $row['title'];?>')"><?php echo $row['title'];?></li>    
<?php } ?>
<?php } ?>

//ajax

$('#inputsch').keyup(function(){
	$.ajax({
	type: 'GET',
	url: 'fetch.php',
	data:'search='+$(this).val(),
	success: function(data){
		$('#box').show();
		$('#box').html(data);
	}
	});
}); 
});
function searchValue(val) {
$('#inputsch').val(val);
$('#box').hide();
}


//search box

 <form action="search.php">
 <input id="inputsch" type="text" name="search" placeholder="search..." autocomplete="off" autofocus>
 <button type="submit" value="search" >search</button> 
 </form>
 <div id="box"></div>


//the problem here is when i click the result is only added to input, but i want it to autosubmit

 

Link to comment
Share on other sites

On 9/29/2019 at 10:42 PM, requinix said:

You don't need to get the value. As far as I know. All you need to do is submit the form. Oh wait, you're not doing that.

Your autocomplete function should put the value into the form. Click the LI, set it as the INPUT's value, and submit the form.

 

On 9/29/2019 at 10:42 PM, requinix said:

You don't need to get the value. As far as I know. All you need to do is submit the form. Oh wait, you're not doing that.

Your autocomplete function should put the value into the form. Click the LI, set it as the INPUT's value, and submit the form.

very helpful thanks

but what if i want to change the

SELECT * FROM posts WHERE title LIKE :search

to

SELECT * FROM posts WHERE title LIKE :search OR  author  LIKE :search;

and want to get both by using single list like

<li class="result" onClick="searchValue('<?php echo $row['some value here'];?>')"><?php echo $row['some value here'];?></li>

here the result on search will be

//title will display on key up

//author will display on key up

instead of 

<li class="result" onClick="searchValue('<?php echo $row['title'];?>')"><?php echo $row['title'];?></li>

<li class="result" onClick="searchValue('<?php echo $row['author'];?>')"><?php echo $row['author'];?></li>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.