For help! Killed 8 hours, the result - 0. There is a code:

<script type="text/javascript"> var tt = 0 var d = new Date(); var n = d.getTime()/1000; var s = <?php echo $_SERVER['REQUEST_TIME']; ?>; var lefin = (n - s)/60/60; if (lefin >= -0.1) { var tt = 1 } if (lefin < -0.1) { var tt = 2 } if (tt == 1) { function ajaxCall(tt) { alert("To AJAX: CHECK "+tt); jQuery.ajax({ type: "POST", url: "wp-content/plugins/UFR/script.php", dataType: 'json', data: "param="+10, success: function(response) { alert("yoyo")} } ); }; ajaxCall(tt); } console.log(n); console.log(lefin); console.log(tt); </script> 

and the script file:

 <?php $timedf = json_decode($_POST['param']); ?> 

According to the idea, the $ timedf variable should now be available on my page - but all is in vain. Who knows what's the matter, help

  • one
    On the page will be available response . And in the php file, I would do echo json_encode(что-то там); - user242433
  • echo json_decode ($ _ POST ['param']); not displayed anyway - Paul Wall
  • data: {"param": 10}, - n.osennij
  • @PaulWall, it should not be displayed. Just then in success: function(response) { alert(response); } success: function(response) { alert(response); } - user242433
  • I need to drag a variable from js to php, maybe I'm not doing it at all? - Paul Wall

2 answers 2

First of all, ajax in wordpress works according to certain rules. You now have direct requests for a file handler, but in WordPress all ajax requests must be sent to /wp-admin/admin-ajax.php , and handlers are connected to hooks. The ajax theme in Wordpress is big enough to describe it here, so read this article to correctly write working supported code.

If you really want your code to work now, try changing the function like this:

 function ajaxCall(tt) { alert("To AJAX: CHECK " + tt); jQuery.ajax({ type: "POST", url: "/wp-content/plugins/UFR/script.php", dataType: 'json', data: { param: 10 }, success: function(response) { console.log(response) } }); } 

And pay attention to the readability of the code and the pairing of brackets

  • Plus and support. - KAGG Design

First, check that the jQuery library is connected. Secondly, in your example, you organize the work of a web application using Ajax, this is the technology of asynchronous interaction with the server, i.e. The file where the js script is located is the client. The script.php file is a server-side script, in theory there should be logic written down what to do with the data transferred from the client.

In your example, you send an Ajax request to the server-side script, the script should do something there, process the received data and return the result that can be obtained in the place where you have written: alert ("yoyo")

 <?php $timedf = json_decode($_POST['param']); // т.е. клиент ждёт ответ в json, конвертируем ответ в JSON и выводим результат echo json_encode($timedf); ?> 
  • The essence of what is happening, the TS understands, but either it does not have WordPress initialization, because it does everything wrong, or else there is some error in the code. - KAGG Design
  • This is WordPress, jQuery is always there - KAGG Design
  • If you are talking about date ('r'), then this function will return the current date, you are mistaken, sorry I can’t take a shit :) - zuvladimir
  • Well, he blinked, yes .., - KAGG Design