开发者

How do I write a simple auto completer for a php form?

I have an html/php form that takes 2 inputs from the user and displays the search results by running a query on the database. How I can enable auto complete for the 2 user inputs where the results are searched from the databa开发者_StackOverflow社区se using AJAX calls?


If 'enable-auto-complete' = Show a possible list of values in a drop down, then there is a jquery plugin with the same name. You can use with an array of values in javascript or in conjunction with Ajax.


Try this(database fetching with multiple fields mydb=data)

//demo_cities.php
<?php

	$con=mysqli_connect("localhost","root","","auto");
	

	$return_arr = array();

    $ac_term = "%".$_GET['term']."%";
    $query = "SELECT * FROM data where name like '$ac_term'";
    
	$result=mysqli_query($con,$query);
    
    while ($row = mysqli_fetch_row($result)) 
	{
        $row_array['label'] = $row[0];
        $row_array['price'] = $row[1];
         
        array_push($return_arr,$row_array);
    }

	echo json_encode($return_arr);

?>
//index.php
</head>

<body>
<form method="post">
<fieldset>
<p><label>Name: </label>
<input type="text" id="name"  name="name" /> <br />

<label>Price: </label>
<input type="text" id="price"  name="price" /> <br /></p>
</fieldset>
</form>

<script>
$(function() {
	$('#price').val("");
				 
	$("#name").autocomplete({
      	source: "demo_cities.php",
        minLength: 1,
        select: function(event, ui) {
            $('#price').val(ui.item.price);
        },
		response: function( event, ui ) 
		{
			$('#price').val("");
		}
    });      
});
</script>
</body>
</html>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜