【问题标题】:PHP and AJAX Error In Receiving Data接收数据时的 PHP 和 AJAX 错误
【发布时间】:2013-09-28 03:47:50
【问题描述】:

我的网站上有一个输入字段。当用户开始在此输入字段中输入他们学校的名称时,下面的 <select> 标签应该会自动执行以下操作:
1. 访问我的 JavaScript 文件(这可行
2. JavaScript 文件应该检索 <input> 的值并将其传递给 PHP 文件(这可行
3. PHP 文件应该接收到已发送的字符串(这可行
4. PHP 文件应该在FetchSchool 类中创建 3 个变量:
5. 第一个变量是_string,它将保存从<input> 传递的字符串的值(这个变量可能是问题的一部分
6. 第二个变量是_output,它将保存要输出到 JavaScript 文件的值(我对这个变量没有任何问题
7. 第三个变量是_DM 变量,它连接到我的DataManager 类(这是一个非常有问题
在我的代码中发生的情况是,在 __construct 函数中声明 _DM 变量之后实际上没有发生任何事情。如果我用//_DM 变量的声明注释掉,那么之后的一切都有效。我检查了DataManager可以FetchSchool 类找到,所以我不知道为什么会发生这个错误。
这是我的代码:

<?php
class FetchSchool {
    public $_string;    // string that will hold the value that is passed in
    public $_output;    // output string of HTML
    public $_DM;
    final public function __construct($_passed_string){
        echo "<option>".$_passed_string."</option>";
        $this->_string = ($_passed_string) ? $_passed_string : "";
        $this->_output = "";
        $this->_DM     = ($_passed_string) ? new DataManager("localhost","root","root","hw_share") : NULL;
        if ($this->_string){
            echo "<option>works</option>";
        } // end if
        else {return "";}
    } // end __construct
} // end class fetchSchool
if (@$_GET["str"]){
    $_fetch_school = new FetchSchool(@$_GET["str"]);
} // end if
?>  

"&lt;option&gt;".$_passed_string."&lt;/option&gt;" 工作并自动显示用户键入的任何内容,但 "&lt;option&gt;works&lt;/option&gt;" 不显示,尽管它应该显示。

【问题讨论】:

    标签: javascript php ajax function class


    【解决方案1】:

    "&lt;option&gt;works&lt;/option&gt;" 不是 echo-ed 的事实要么是因为 $this-&gt;_string 是空的,要么是该行有问题

    $this->_DM  = ($_passed_string) ? new DataManager("localhost","root","root","hw_share") : NULL;
    

    这使 php 脚本停止运行。

    由于在这种情况下$this-&gt;_string 似乎不是空的,我很确定当您在该行创建 DataManager 实例时出现了问题。

    您必须发布创建类DataManager 的代码,否则我们无能为力。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-13
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多