1

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>&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); ?>
Dhay
  • 585
  • 7
  • 29
  • Possible duplicate of http://stackoverflow.com/questions/18697034/how-to-pass-parameters-in-ajax-post – Vikash Jun 22 '15 at 14:02
  • Are you including jQuery anywhere? – jeroen Jun 22 '15 at 14:03
  • @jeroen: No. the above content is the whole thing. – Dhay Jun 22 '15 at 14:05
  • 2
    Well, you can't run jQuery functions like `$.post()` without including jQuery first. – jeroen Jun 22 '15 at 14:05
  • Thanks. Let me include jQuery. I will search how to include it. – Dhay Jun 22 '15 at 14:08
  • @jeroen: OK. I've included it. Is there any more I need to do? Because I still don't get temp file. I've updated **g.htm** – Dhay Jun 22 '15 at 14:13
  • 1
    You should open the browser console and check if you post variables to PHP file, than you need to check if you get those variables in PHP, than to check if your PHP file has permission to write other files... – skobaljic Jun 22 '15 at 14:18
  • @skobaljic: Thanks. I get 'no element found' for both my.php and 'g.htm'. I changed type="text/javascript" to language="javascript". tried. Then changed $_POST to $_GET in php with no luck. I don't know if I am incapable of this since I am searching for this about 8 hours. – Dhay Jun 22 '15 at 14:31
  • 1
    I am sure you can do it, maybe read some tutorials/examples first: [Beginner's Guide to AJAX](http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php), [AJAX with JSON return](https://jonsuh.com/blog/jquery-ajax-call-to-php-script-with-json-return/), [jQuery AJAX write to file on SO](http://stackoverflow.com/questions/16258750/jquery-ajax-save-to-file) – skobaljic Jun 22 '15 at 15:05
  • @skobaljic: Thank you for your kind words and thank you for the links – Dhay Jun 22 '15 at 16:36
  • @skobaljic And thank you for teach me to fish. – Dhay Jun 23 '15 at 03:59

2 Answers2

2

Add jQuery library in the <head> first:

<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({
  type: "POST",
  url: "my.php",
  data: { postres: res }
});
Dhay
  • 585
  • 7
  • 29
alexandreferris
  • 662
  • 1
  • 11
  • 29
  • 1
    whew! You saved my life for the thing that I was hitting the wall for hours with $.post. Thank you. – Dhay Jun 22 '15 at 16:43
1

Try $temp = sys_get_temp_dir(); in your php file.

James
  • 70
  • 1
  • 9