0
  $( "#autocomplete" ).autocomplete({
    source: [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ]
  }); // this is the simple version. LOOK TO THE UPDATE!!!

这是我的 JQuery 代码。

问题:如何使用 PHP 创建编程语言的向量!?

更新:

jQuery

var keys = null; // HERE I NEED THE ARRAY, ANY ARRAY GENERATED USING PHP

$("#word").autocomplete({
    source: function(req, response) { 
    var re = $.ui.autocomplete.escapeRegex(req.term); 
    var matcher = new RegExp( "^" + re, "i" ); 
        response($.grep( keys, function(item){ 
            return matcher.test(item); }) );
     },
    minLength: 2,
    select: function(event, ui) {
        $('#word').val(ui.item);
    }
});

html

 <form method="POST" name="search" action="">
    <label for="autocomplete">Im looking for:</label>

    <input type="text" id="word" name="word" /> 
    <input type="hidden" id="word_id" name="word_id" />
 </form>

php

   for($i = 1; $i < $total_rows; ++$i) 
   {
      $sample_return[] = array("id"=>$i,"label"=>$ar1[$i],"value"=>$ar2[$i]);
   }
4

3 回答 3

1

您可以使用json_encode.

在这种情况下:

source: <?php echo json_encode(Array("c++","java"...)); ?>
于 2013-06-03T23:13:53.077 回答
1

这是我放在一起的一个快速示例,它显示了您真正尝试做的事情(最终),最好使用 ajax 查询将用作自动完成的“feeder 文件”source:

而不是直接将所有值呈现/回显到页面中:

<?php 
//Check request is from a ajax request
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'){
    //check the term passed to this php from jquery autocomplete 
    if(!empty($_GET['term']) && strlen(trim($_GET['term'])) >= 2){
        //This is where you would query your db for the values using a LIKE%

        $sample_return = array(
        array('id'=>1,'label'=>"C++",'value'=>'c'),
        array('id'=>2,'label'=>"Java",'value'=>'Java'),
        array('id'=>3,'label'=>"PHP",'value'=>'php'),
        array('id'=>4,'label'=>"JavaScript",'value'=>'javascript'),
        array('id'=>5,'label'=>"ASP",'value'=>'asp'),
        array('id'=>6,'label'=>"Ruby",'value'=>'ruby'),
        );
        //boom
        die(json_encode($sample_return));
    }
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/themes/black-tie/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/jquery-ui.js"></script>
</head>

<body>
<script>
$(function() {
    $( "#autocomplete" ).autocomplete({
        source: "autocomplete.php",
        minLength: 2,
        select: function( event, ui ) {
            alert('Do something with: ID:' + ui.item.id + ' NAME:' + ui.item.label+' VALUE:' + ui.item.value);
        }
    });
});
</script>

<form method="POST" name="search" action="">
  <label for="autocomplete">Im looking for:</label>
  <input type="text" id="autocomplete" name="search" size="50">
  <input type="button" class="button" value="Search"/>
</form>

</body>
</html>
于 2013-06-03T23:25:44.420 回答
0

你应该json_encode

$langs = array( "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby");

echo json_encode($langs); // this will produce the desired output

但是请不要将它注入到您的 js 代码中,而是这样做:

<script type="application/json" id="langs">
   <?php echo json_encode($langs); ?>
</script>    

$( "#autocomplete" ).autocomplete({
  source: $.parseJSON($('#langs').text());
});

通过这种方式,您可以将 JS 代码与数据源分离。

于 2013-06-03T23:13:39.927 回答