javascript - Populate Datalist from the Database -


am new javascript , php want populate datalist mysql table have difficulties relevant codes presented below.i able fetch data database not able display them in datalist element.

the html code

<form class="form" action="insert.php" method="post" name="access_form">    <ul>    <li>  <h2>please fill form</h2>    </li>    <li>       <label for="firstname">first name</label>        <input name="firstname" id="keyword" type="text" placeholder="type first name (required)" required />        <datalist id="results">  	  </datalist>  </li>

this javascript code

var min_length = 1;    $( document ).ready(function() {  	$("#keyword").keyup(function() {  		var keyword = $("#keyword").val();  		if (keyword.length >= min_length) {  			$.get( "auto-complete.php", { keyword: keyword } )  			.done(function( data ) {  				$('#results').html('');  				var results = jquery.parsejson(data);  				$(results).each(function(key, value) {  					$("datalist").empty();                                          $("datalist").html(data);  				})    			    $('.item').click(function() {  			    	var text = $(this).html();  			    	$('#keyword').val(text);  			    })    			});  		} else {  			$('#results').html('');  		}  	});        $("#keyword").blur(function(){      		$("#results").fadeout(500);      	})          .focus(function() {		      	    $("#results").show();      	});    });

the php code below

  <?php   require('constant.php');    require('database.php');    if (!isset($_get['keyword'])) {       die();     }    $keyword = $_get['keyword'];   $data = searchforkeyword($keyword);   echo json_encode($data);   ?> 

please suggestions

it looks $.each going empty dataset every result.

try instead.

$("datalist").empty(); $(results).each(function(key, value) {    $("datalist").append($("<option>").innerhtml(data));     }) 

you'll creating new option element , appending datalist element.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -