there is a JS variable var str = 15;

You need to pass it to the PHP script and assign the number 15 another variable, say $str

As I understood it is possible only with the help of AJAX, and I don’t understand it at all

Help, somebody, the task is simple, but the solution is beyond my powers

  • Well, it was necessary not to skip the extension, there they just chewed on the basics of adjax, thickly smearing adjika ... - Sergey V.

2 answers 2

on jQuery :

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> var str = 15; $.ajax({ url: '/test.php', type: "POST", data: {post_data: str}, success: function(data) { console.log(data); } }); </script> 

Then your number 15 will come to the test.php script, and process it like this:

 <?php echo $_POST['post_data']; // ну или другие манипуляции 

An example at startup will display your number in the console console. If the output - works :)

  • Manitikyl @ console says: Failed to load generator: 91 / index.php : No 'Access-Control-Allow-Origin' header is present on the. Am I doing the right thing by accessing the script via http? I just have no idea) - Nikolai Vasilenkov
  • Manitikyl @ added header script ('Access-Control-Allow-Origin: *'); the error disappeared, but the console doesn’t say anything - Nikolay Vasilenkov

jQuery

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> var str = 15; $.ajax({ url: '/test.php', type: "POST", data: {post_data: str}, success: function(data) { console.log(data['value']); } }); </script> 

test.php

 <?php header('Content-Type: application/json'); $data = $_POST['post_data']; $response = array(); $response['value'] = $data; echo json_encode($response); ?> 

It is desirable to structure the data, rather than throwing them with a bare echo. json is best suited for this.

  • gut you kid loaded !!! he just-only adzhiki ate, and you whip him with a jison ....))) - Sergey V.