[英]Pass variable from Javascript to PHP to write to file
It could be duplicate cause unable to find answer even by searching similar questions of Stackoverflow. 它可能是重复的,因为即使通过搜索Stackoverflow的类似问题也无法找到答案。 This is what I am trying to do.
这就是我想要做的。 I need to pass a stored variable in JavaScript to PHP which would write it to temp file 'yy.html'.
我需要将JavaScript中存储的变量传递给PHP,然后将其写入临时文件'yy.html'。 I could get alert from the submit function but not the temp file.
我可以从提交功能获得警报,但不能获得临时文件。 I get temp file when I run PHP alone.
当我单独运行PHP时,我得到临时文件。
g.htm 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>  ';
function submit()
{
alert(res);
$.post('my.php', { postres: res} );
}
</script>
</body>
</html>
my.php my.php
<?php $res = $_POST['postres']; $temp = getenv("TEMP"); file_put_contents($temp . "/yy.html", $res); ?>
Add jQuery library in the <head>
first: 首先在
<head>
添加jQuery库:
<head>
..
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js/jquery.min.js">
..
</head>
Then, try this with Ajax: 然后,使用Ajax尝试:
$.ajax({
type: "POST",
url: "my.php",
data: { postres: res }
});
Try $temp = sys_get_temp_dir(); 试试$ temp = sys_get_temp_dir(); in your php file.
在你的PHP文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.