i have a combo box in html and i wish to show data comming from database in it by manipulating it to if database result have abc/cde/etc and user put a these all will display i did this through jquery but couldnt find a dynamic way to paas values..
enter code here
<?php
// Connects to your Database
$connection=mysql_connect("localhost","root","");
$db="kukreja";
mysql_select_db($db,$connection) or die("could not open $db");
$sql="SELECT*FROM add_item";
$result=mysql_query($sql,$connection) or die("could not execute sql:$sql");
$num_result=mysql_fetch_Array($result);
?>
<script type="text/javascript">
jQuery(function($) {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
Use jQuery ajax . Let's check the bellow example
I really would suggest that you avoid to use the mysql_
functions when writing new code. Please do read this question.
That being said:
If the values can change and need to be updated dynamically during the user's session, you should use jQuery and AJAX as suggested by vijay4vijju.
But if not, you can save a request-response roundtrip to your server this way:
First, in the PHP section
$availableTags = array();
while ( $tagData = mysql_fetch_array($result) ) {
$availableTags[] = $tagData[0]; // Or whatever index you have the tag at
}
Then, in the JavaScript section
var availableTags = <?php echo json_encode($availableTags); ?>;
You probably need to adopt this to your situation. And you definitely should change your code to use mysqlii
or PDO
.