-2

I want to pass javascript variables (without form) to php, so php can write them in a textfile on the server-side. For this, I'm using this jQuery line:

$.post( "http://localhost/main.php", data);

Here is the php:

$data = $_POST['data'];
echo $data;

$file = "test.txt"
$handle = fopen($file, 'w');
fwrite($handle, $data);
fclose($handle);

I think the problem could be the 'data' tag, when I try to access the information in php, but I don't know what should I put there since all the examples that I found was associated with html forms.

The other thing tho', which I don't really understand is: I do this whole thing on a button-click, so if I click the button, it posts to the php, but does that actually run the above php code, and writes in the text file or am I missing something?

*btw, I use WAMP for PHP, but I don't think it matters.

hotigeftas
  • 151
  • 1
  • 3
  • 9

2 Answers2

1

Post data is submitted in key/value pairs. For your payload that's:

hours => 0, mins => 0, secs => 0, tenths => 0

After the POST request PHP has this data inside of the $_POST associative array. To access hours for example you'd write $hours = $_POST["hours"]. If you want to write the data to the text file you now need to tell PHP what the file should look like:

<?php
$hours = $_POST["hours"],
$mins = $_POST["mins"]
.
.
.

$fileDataToWrite = "Hours: ".$hours." Minutes: ".$mins // and so on

This variable can now be passed to the fwrite function as the second parameter:

$file = "test.txt"
$handle = fopen($file, 'w');
fwrite($handle, $fileDataToWrite);
fclose($handle);

The resulting file would then look like this:

Hours: 0 Minutes: 0

You can choose how you want the file to look like, or if you for example want it to be in JSON format by simply changing how you build the $fileDataToWrite variable.

In your JS code you should add a callback for when the PHP has ran on the server side:

$.post("http://localhost/main.php", data, 
    response => console.log("Time saved on the server!"));
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
1

Semi-Copying my answer from here

PHP is kind of interesting in that it doesn't pull from $_POST like other forms when Ajax is involved. You actually will need to read the input from php://input

Here is a tiny example

$data = file_get_contents("php://input");
$response = json_decode($data, true ); // True converts to array; blank converts to object

$emailAddr = $response["hour"];

Hopefully you can apply that successfully.

While debugging this I would highly recommend using Chrome's Developer mode with the 'network' tab. Find your ajax call near the bottom and you can view exact header info.

John Pavek
  • 2,595
  • 3
  • 15
  • 33
  • Thank you for your answer. When I send the POST request, I can console out this on the client-side: Time saved on the server!{"hours":"00","mins":"00","secs":"01","tenths":"2"}. Then the PHP gets the request, as I can see it in the headers, and it also has 200 status code. Then I try to var-dump it, and it says: C:\wamp64\www\phpsandbox\timePlusPlus\main.php:4:string '' (length=0). The code looks like this: $data = file_get_contents("php://input"); var_dump($data); Do you have any idea, why this happening? Note, that on client-side it says POST, but on the server it says it's a GET. – hotigeftas Feb 27 '18 at 23:39
  • @hotigeftas if Php is reading it as empty it's likely the request isn't coming through properly, I would watch the network tab as you submit this and see how the headers look, and ensure the data object is passing properly. – John Pavek Feb 28 '18 at 14:02