将包含数据库的php数组传递给jquery进行操作

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>

2个回答



使用jQuery ajax。 让我们看看下面的示例</ p>

示例 </ p >
</ div>

展开原文

原文

Use jQuery ajax . Let's check the bellow example

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问