简体   繁体   English

将JavaScript文件设置为PHP中的变量

[英]Set JavaScript file to a variable in PHP

Okay so I'm trying to set a JavaScript document to a variable in PHP? 好的,我想将JavaScript文档设置为PHP中的变量吗?

Essentially I'm setting the WiFi speed I calculate in a Javascript document to a variable,so I can save the variable value in a database with other information as an instance. 本质上,我将在Javascript文档中计算的WiFi速度设置为一个变量,因此我可以将变量值与其他信息一起作为实例保存在数据库中。

The Javascript code is pretty long so I don't know if I should copy the whole code in and set it equal to the variable or if there's a syntax to set it to a variable. Javascript代码很长,因此我不知道是否应该复制整个代码并将其设置为等于变量,或者是否有语法将其设置为变量。

I've seen: 我见过:

<script type="text/javascript" src="file.js"></script>

Online for calling a Javascript file but not sure how to get that value and store it in a variable. 在线调用Javascript文件,但不确定如何获取该值并将其存储在变量中。

You could do something like this 你可以做这样的事情

$js = file_get_contents( 'http://www.example.com/javacsript.js');

$value = trim( str_replace( array( "document.write('", "');"), '', $js));

echo $value;

Hope this will help you 希望这个能对您有所帮助

The JavaScript must be executed in the browser client. JavaScript必须在浏览器客户端中执行。 The flow would be: 流程为:

  1. PHP generates HTML (wich includes the JS code) PHP生成HTML(其中包括JS代码)
  2. HTML is sent to the browser HTML发送到浏览器
  3. The browser renders the HTML and executes the JS 浏览器呈现HTML并执行JS
  4. The browser communicates with the server to tell the result 浏览器与服务器通信以告知结果

Depending if the JS is a library or an script the precise steps would differ. 根据JS是库还是脚本,精确的步骤会有所不同。 But basically inside tags you will have to save the result to a variable and then make an AJAX call (easier with jQuery.ajax() ) to communicate that variable to the server and then the server can do something with it. 但是基本上在标签内部,您将必须将结果保存到变量中,然后进行AJAX调用(使用jQuery.ajax()更为轻松),以将该变量传递给服务器,然后服务器可以对其进行处理。

I hope that helps puting you on the right track. 我希望这有助于您走上正确的道路。 If you expand the info in your question, I will try to update my answer :) 如果您扩展问题中的信息,我将尝试更新答案:)

You have to do this using a POST, possibly to the same PHP script. 您必须使用POST来执行此操作,可能要使用相同的PHP脚本。

<form method='post' id=myform>
 <input type=hidden id=js-to-php value=0>
</form>
<script>
jQuery(document).ready(function(){
 //calulcate the wifispeed using the long js code
 //then save it in the field
 $('#js-to-php').val(YOUR_SPEED);
 // send the form
 $('#myform').submit();
});
</script>

and then in the same script: 然后在相同的脚本中:

if( isset($_POST['js-to-wifi']) && $_POST['js-to-wifi']!='') {
  // store your stuff in DB
}

So here is the Java script code: 因此,这是Java脚本代码:

//Source: http://stackoverflow.com/questions/5529718/how-to-detect-internet-speed-in-javascript


var imageAddr = "http://www.kenrockwell.com/contax/images/g2/examples/31120037-5mb.jpg"; 
var downloadSize = 4995374; //bytes

window.onload = function() {
    var oProgress = document.getElementById("progress");
    oProgress.innerHTML = "Loading the image, please wait...";
    window.setTimeout(MeasureConnectionSpeed, 1);
};

function MeasureConnectionSpeed() {
    var oProgress = document.getElementById("progress");
    var startTime, endTime;
    var download = new Image();
    download.onload = function () {
        endTime = (new Date()).getTime();
        showResults();
    }

    download.onerror = function (err, msg) {
        oProgress.innerHTML = "Invalid image, or error downloading";
    }

    startTime = (new Date()).getTime();
    var cacheBuster = "?nnn=" + startTime;
    download.src = imageAddr + cacheBuster;

    function showResults() {
        var duration = (endTime - startTime) / 1000;
        var bitsLoaded = downloadSize * 8;
        var speedBps = (bitsLoaded / duration).toFixed(2);
        var speedKbps = (speedBps / 1024).toFixed(2);
        var speedMbps = (speedKbps / 1024).toFixed(2);
        oProgress.innerHTML = "Your connection speed is: <br />" + 
           speedBps + " bps<br />"   + 
           speedKbps + " kbps<br />" + 
           speedMbps + " Mbps<br />";
    }
}

I want to get the value that this will return (I will edit the code so I am only getting one value) and then place it inside a php variable. 我想获取将返回的值(我将编辑代码,所以我只得到一个值),然后将其放在php变量中。 The issue is when I run it on a webpage after using: 问题是当我使用以下命令在网页上运行它时:

$Speed = file_get_contents( 'wiFiCalc.js');
$value = trim( str_replace( array( "document.write('", "');"), '', $Speed));
echo $value;

I just get the code on the html page, as clami219 stated above. 正如上面提到的clami219一样,我只是在html页面上获得了代码。 I just want to return that value to print it and store it in the database. 我只想返回该值以打印它并将其存储在数据库中。

Also, Jobst, the way you wrote was kind of hard to follow. 同样,乔布斯特,你的写作方式很难遵循。 I am using a form action in my html code to go to the speed so it can be stored in the database before it returns to the next HTML page so could you explain how your code works? 我在html代码中使用了表单操作以加快速度,以便可以在返回下一个HTML页面之前将其存储在数据库中,因此您可以解释一下代码的工作方式吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM