php 类和 jQuery

标签 php jquery

我有简单的自动完成 JQuery

$(document).ready(function() {
$( ".autocomplete" ).each(function() {
    $(this).autocomplete({
        source: '/php/LiveSearch2.php?name='+ $(this).attr('name') + '&',
        minLength: 1,
        appendTo: "#container"
    }); 
});
});

我已经编写了 php 类来推送值,但它不起作用。我在某个地方犯了一些错误。下面是LiveSearch2.php

<?php

class swapnil {

private $testname;
public $json;

public function __construct {
    $this->testname = array('swapnil','ranadive');
    $this->json = json_encode($this->testname);
    echo $this->json;

}
}

$Business = new swapnil();
?>

当我使用没有类的 php 代码时,它工作正常。

<?php
 $testname = array('swapnil', 'ranadive');
 $json = json_encode($testname);

 echo $json
 ?>

最佳答案

__construct 之后缺少括号。这里试试这个:

 class swapnil {

 private $testname;
 public $json;

 public function __construct() {
  $this->testname = array('swapnil','ranadive');
  $this->json = json_encode($this->testname);
  echo $this->json;

 }
}

$Business = new swapnil();

关于php 类和 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40624660/

相关文章:

Jquery Accordion 删除边框顶部

php - 只显示数据库中的特定内容

php - 设置一个 cookie 供 cURL 使用

php - 在哪里使用 Zend_Auth,在模型或 Controller 中?

javascript - 使用 qTip 工具提示通过 AJAX 加载数据。 $(this) 不是我期望的那样

php - 如何将 php 变量传递给 ajax

PHP 压缩来自 Gopro 的视频,最好的方法?

javascript - 通过 Json 传输表单数据

javascript - 使 float 的 div 随页面向上/向下滚动?

javascript - 如何在单击 3 次鼠标后启动操作