是否有任何方法可以在同一文件中访问PHP中的JS变量?
我有这个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法访问这样的JavaScript变量。 PHP由服务器执行,客户端执行。您可以在JS中创建一个调用PHP脚本的AJAX函数。
教程: http://wwwww.w3schools.com/ajax/ajax/ajax/
>
php
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
PHP