【问题标题】:How to load php vars from an external file with javascript如何使用javascript从外部文件加载php vars
【发布时间】:2016-02-24 21:14:50
【问题描述】:

我在显示信使的页面上的 <div id="chtmsg"> 中有此代码...

PHP:

if($perguntas){
  for($c=0;$c<count($perguntas);$c++){
    $perguntas[$c]->tipo == 'F' ? $class = 'message_F' : $class = 'message_P';
    $hora = substr($perguntas[$c]->hora, 0, 5);
    echo "<li class=\"".$class."\"><p>".$perguntas[$c]->mensagem."</p><span>".$pergunta->databr($perguntas[$c]->data)." - ".$hora."</span></li>";
    if($perguntas[$c]->tipo=='F' and $perguntas[$c]->status == 0){
      $pergunta->marcaRespLida($perguntas[$c]->id);
    }
  }
}

效果很好。因此,我想用 js 加载它以仅在 div #chtmsg 内刷新所有新消息,然后我创建了一个文件 msg.php 并使用 &lt;?php include("msg");?&gt; 它继续运行良好,但使用 js 我需要放置路径。 ..

HTML:

$(document).ready(function () {
    setInterval(function() {
        $.get(hostGlobal+'site/modulos/produto/msg.php', function (result) {
            $('#chtmsg').html(result);
            scTop();
        });
    }, 3000);
});

但它显示了 de div 内部的错误...

注意:未定义变量:第 3 行 /Applications/XAMPP/xamppfiles/htdocs/sisconbr-sistema-novo/site/modulos/produto/msg.php 中的 perguntas

我测试了 msg.php 文件中的其他代码,并且在没有变量的情况下可以正常工作...

【问题讨论】:

  • 你在哪里定义$perguntas变量?你能在msg.php里面定义它吗?
  • 其实你的变量没有定义。如果您在浏览器中调用 php 文件,您将看到相同的消息。您需要在 $.get 查询中将变量作为参数发送。

标签: javascript php mysql variables


【解决方案1】:

只是一个想法......

你在 PHP 中的第一行

if($perguntas){

也许应该检查是否像这样定义

if(isset($perguntas)){

我的建议在另一个答案here中解释了

【讨论】:

    【解决方案2】:

    为了更好的代码,你最好使用:

    if (isset($perguntas) && is_array($perguntas)){
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多