【问题标题】:Load return of PHP function into an HTML textbox将 PHP 函数的返回值加载到 HTML 文本框中
【发布时间】:2012-02-11 06:23:50
【问题描述】:

PHP 类

<?php

class food {
    public function __construct ($parameter){
        $path = array("something") ;
        $this->getPath($path);
    }
    protected function getPath ($array){
        $json = json_encode($array);
    }
}

$print = new food(x);

HTML

<!-- ... -->
<form name="form1" action="" method="GET" >
 <input type="text" id="hiddenbox" value="<?php $json ?>" />
</form>
<!-- ... -->

我想将$json 值加载到文本框中。但是,不显示该值。如何将$this-&gt;getPath() 的返回输入到文本框中?

【问题讨论】:

  • 我不会重新编辑以使其再次可读:P 提示:编辑器中的 {} 按钮将为您格式化代码,以便正确显示。
  • hmmm.. 哪个 {} 按钮?它是一个html按钮标签
  • 我不确定你想在这里实现什么。

标签: php class function


【解决方案1】:

我假设您想在 food 类中创建一个字段 $json。以下是您的 getPath 函数的外观:

protected function getPath ($array){
    $this->json = json_encode($array);
}

然后,您需要将 value ="&lt;?php $json ?&gt;" 替换为以下内容(使用您实例化的 $print 对象):value ="&lt;?php echo htmlspecialchars($print-&gt;json); ?&gt;"htmlspecialchars 位特别重要,因为它“将特殊字符转换为 HTML 实体”。

【讨论】:

    【解决方案2】:

    首先,您的班级名称是$print,而不是$json。其次,要在类中运行函数,您必须编写 $print-&gt;getPath($array) (因此还要在某处定义 $array )。第三,你没有在getPath()函数中回显任何东西,所以你不能只写$json或那里的函数调用,你需要让getPath'回显'它或返回$json变量。

    简而言之?这个问题的格式很糟糕。

    【讨论】:

      猜你喜欢
      • 2015-04-20
      • 1970-01-01
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多