gpt4 book ai didi

javascript - 将变量从 Javascript 传递到 PHP 以写入文件

转载 作者:行者123 更新时间:2023-11-30 12:21:16 25 4
gpt4 key购买 nike

即使在 Stackoverflow 上搜索类似问题也无法找到答案,这可能是重复的。这就是我想要做的。我需要将一个存储在 JavaScript 中的变量传递给 PHP,PHP 会将其写入临时文件“yy.html”。我可以从提交功能中获得警报,但不能从临时文件中获得警报。当我单独运行 PHP 时,我得到了临时文件。

g.htm

<html>
<head><title>Hide and Seek</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js/jquery.min.js">
</script>
</head>
<body>
<span id = "nextbutton"></span>
<script type="text/javascript">
res = "Hide & Seek";
document.getElementById('nextbutton').innerHTML = '<button type="submit" class="btn-u btn-u-default" OnClick="javascript:submit()">Restart</button>&nbsp;&nbsp';
function submit()
{
alert(res);
$.post('my.php', { postres: res} );
}
</script>
</body>
</html>

my.php

<?php $res = $_POST['postres']; $temp = getenv("TEMP"); file_put_contents($temp . "/yy.html", $res); ?>

最佳答案

<head> 中添加 jQuery 库第一:

<head>
..
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js/jquery.min.js">
..
</head>

然后,用 Ajax 试试这个:

$.ajax({
type: "POST",
url: "my.php",
data: { postres: res }
});

关于javascript - 将变量从 Javascript 传递到 PHP 以写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30981915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com