Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Javascript获取PHP脚本输出?_Javascript_Php_Jquery_Html_Web - Fatal编程技术网

如何从Javascript获取PHP脚本输出?

如何从Javascript获取PHP脚本输出?,javascript,php,jquery,html,web,Javascript,Php,Jquery,Html,Web,这是我希望从javascript文件中获取输出的PHP脚本的一个示例: data.php 我只是想要一种测试方法,看看存储在php变量中的data.txt文件中的数据是否可以传递到javascript文件中,然后在html页面上的javascript控制台中打印出来 我想这样做,以便我可以在文本文件中存储一个变量,然后在多个用户同时动态更新时引用它 我已经看到了实现这一点的方法,但它涉及到javascript与html位于同一个文件中,这里的情况并非如此。我也在使用jquery,所以我不知道这是

这是我希望从javascript文件中获取输出的PHP脚本的一个示例:

data.php 我只是想要一种测试方法,看看存储在php变量中的data.txt文件中的数据是否可以传递到javascript文件中,然后在html页面上的javascript控制台中打印出来

我想这样做,以便我可以在文本文件中存储一个变量,然后在多个用户同时动态更新时引用它


我已经看到了实现这一点的方法,但它涉及到javascript与html位于同一个文件中,这里的情况并非如此。我也在使用jquery,所以我不知道这是否有什么不同。我以前从未使用过php,对javascript也不熟悉,因此非常感谢您的帮助。

您可以使用jquery直接读取该文本文件,如下所示:

    $.ajax({
        url : "data.txt",
        dataType: "text",
        success : function (data) {
            // Display the data in console
            console.log(data);
            // Or append it to body
            $('body').append(data);
        }
    });
与读取php文件输出的方式相同,在这种情况下,您应该更改url以指向php文件。您应该了解的另一件事是,与服务器客户端通信的不同选项,如json数据结构等。
文档:

如果将扩展名更改为“php”,则可以将php代码放入javascript文件中。由于“php”扩展在默认情况下将以Html形式交付,因此您必须在代码中声明它是Javascript

script.js.php

<?php header('Content-Type: application/javascript');
?>console.log("<?php

$input = file_get_contents('data.txt');
echo $input."\n";

 ?>");
$(document).ready(function(){


 $("#imgTag, #img2").on("click", process);

 var size = 0;

 function getTarget(evt)
 {
    evt = evt || window.event;
    return evt.target || evt.scrElement;
  }

  var temp;
  console.log("before get");
  console.log("post get");
  console.log(size);

  function changeSize(myName, myOther)
  {
     var name = myName;
    var other = myOther;
    if($("#" + name).height() < 400)
    {
       $("#" + name).height($("#" + name).height() + 5);
       $("#" + name).width($("#" + name).width() + 5);
       $("#" + other).height($("#" + other).height() - 5);    
       $("#" + other).width($("#" + other).width() - 5);   
    }    
  }

  function process(event)
  {
     var name = getTarget(event).id;
     var other;
     if(name == "imgTag")
     {
        other = "img2";
     }
    else
      other = "imgTag";

console.log($("#" + name));
console.log("Changing size!!!");
console.log( $("#" + name).height());
changeSize(name, other);
}
});
console.log(“”);
$(文档).ready(函数(){
美元(“#imgTag,#img2”)。点击(“点击”,流程);
变量大小=0;
函数getTarget(evt)
{
evt=evt | | window.event;
返回evt.target | | evt.screlation;
}
无功温度;
console.log(“获取前”);
console.log(“post-get”);
控制台日志(大小);
函数changeSize(myName,myOther)
{
var name=myName;
var-other=myOther;
如果($(“#”+name).height()<400)
{
$(“#”+name).height($(“#”+name.height()+5);
$(“#”+名称).width($(“#”+名称).width()+5);
$(“#”+其他).height($(“#”+其他).height()-5);
$(“#”+其他).width($(“#”+其他).width()-5);
}    
}
功能流程(事件)
{
var name=getTarget(事件).id;
var其他;
如果(名称==“imgTag”)
{
其他=“img2”;
}
其他的
其他=“imgTag”;
console.log($(“#”+name));
log(“更改大小!!!”;
console.log($(“#”+name.height());
更改大小(名称、其他);
}
});
    $.ajax({
        url : "data.txt",
        dataType: "text",
        success : function (data) {
            // Display the data in console
            console.log(data);
            // Or append it to body
            $('body').append(data);
        }
    });
<?php header('Content-Type: application/javascript');
?>console.log("<?php

$input = file_get_contents('data.txt');
echo $input."\n";

 ?>");
$(document).ready(function(){


 $("#imgTag, #img2").on("click", process);

 var size = 0;

 function getTarget(evt)
 {
    evt = evt || window.event;
    return evt.target || evt.scrElement;
  }

  var temp;
  console.log("before get");
  console.log("post get");
  console.log(size);

  function changeSize(myName, myOther)
  {
     var name = myName;
    var other = myOther;
    if($("#" + name).height() < 400)
    {
       $("#" + name).height($("#" + name).height() + 5);
       $("#" + name).width($("#" + name).width() + 5);
       $("#" + other).height($("#" + other).height() - 5);    
       $("#" + other).width($("#" + other).width() - 5);   
    }    
  }

  function process(event)
  {
     var name = getTarget(event).id;
     var other;
     if(name == "imgTag")
     {
        other = "img2";
     }
    else
      other = "imgTag";

console.log($("#" + name));
console.log("Changing size!!!");
console.log( $("#" + name).height());
changeSize(name, other);
}
});