是否有任何方法可以在同一文件中访问PHP中的JS变量?

发布于 2025-02-13 21:36:49 字数 799 浏览 1 评论 0原文

我有这个HTML文件,用于将字符串加密到SHA1 HASH。我从JavaScript函数(我尚未显示下面的功能)接收到“ 1 | 2 | 3”的字符串,我需要替换|使用 - 然后对修改后的字符串进行加密。这是我到目前为止构建的代码 -

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
    var test;
    var value;
    var message = "1 | 2 | 3";
    test = message.split(' | ');
    value = "<?php $final = test[0] . " - " . test[1] . " - " . test[2]; echo sha1($final);?>";
    alert(value);
</script>
</body>
</html>

我开始知道使用html表单将值发布到另一个PHP文件,并通过$ _ POST/$ _ get - 但是我的应用程序不允许2个文件;因此,我试图将其全部组合到一个HTML文件中...


PHP具有内置的SHA1加密函数,但是JavaScript却没有 - value上述代码中的变量是破碎的;如何将变量从JS传递给PHP,从PHP回到JS?同样,我只想使用PHP修改(对其进行加密)JS变量,然后将PHP修改变量的值设置为新的JS变量。我应该怎么办?请指导:)

I have this html file which I am using to encrypt a string to sha1 hash. I receive the string like "1 | 2 | 3" from a javascript function (I haven't shown the function below), and I need to replace the | with - and then encrypt the modified string. Here's the code I have built so far -

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
    var test;
    var value;
    var message = "1 | 2 | 3";
    test = message.split(' | ');
    value = "<?php $final = test[0] . " - " . test[1] . " - " . test[2]; echo sha1($final);?>";
    alert(value);
</script>
</body>
</html>

I came to know about posting the value to another PHP file using HTML form, and accessing it through $_POST/$_GET - but my app won't allow for 2 files; hence I am trying to combine it all in one html file...


PHP has a in-built SHA1 encryption function, but javascript doesn't - the value variable in the above code is broken; how do I pass a variable to PHP from JS and back from PHP to JS? Again, I just want to modify (encrypt it) a JS variable using PHP and then set the PHP modified variable's value to a new JS variable. What should I do? Please guide :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海夕 2025-02-20 21:36:49

您无法访问这样的JavaScript变量。 PHP由服务器执行,客户端执行。您可以在JS中创建一个调用PHP脚本的AJAX函数。

教程: http://wwwww.w3schools.com/ajax/ajax/ajax/

>

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
       xhttp.responseText // return from your PHP;
    }
  };
 xhttp.open("GET", "yourphp.php?variable="+yourjsvariable, true);
 xhttp.send();
}

php

<?PHP

return $_GET["variable"]; 

?>

You can't access to javascript variable like that. PHP is executed by the server and JavaScript by the client. You can create an ajax function in JS that call your PHP script.

Tutorial: http://www.w3schools.com/ajax/

JS

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
       xhttp.responseText // return from your PHP;
    }
  };
 xhttp.open("GET", "yourphp.php?variable="+yourjsvariable, true);
 xhttp.send();
}

PHP

<?PHP

return $_GET["variable"]; 

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文