It could be duplicate cause unable to find answer even by searching similar questions of 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'. I could get alert from the submit function but not the temp file. I get temp file when I run PHP alone.
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
<?php $res = $_POST['postres']; $temp = getenv("TEMP"); file_put_contents($temp . "/yy.html", $res); ?>