How to save or transfer the value of the handler's php function to the moment the ajax request is triggered by the server, another function inside function.php until the response is sent?

I tried through global, pass a parameter to another function, just write to a file on the server, but this does not work. The ajax itself paired with the function works fine, but how at the moment of the php response on the server to catch and transfer what came with ajax further, I can not understand, is it possible to intercept this data?

AJAX request to server:

$(".submit-button").on("click", function (event) { event.preventDefault(); if($("#one__q1").attr("checked") == 'checked') { var answer11 = $("#one__q1").val(); } if($("#one__q2").attr("checked") == 'checked') { var answer12 = $("#one__q2").val(); } if($("#one__q3").attr("checked") == 'checked') { var answer13 = $("#one__q3").val(); } // var answer11 = $("#one__q1").val(); // var answer12 = $("#one__q2").val(); // var answer13 = $("#one__q2").val(); $.ajax({ url: "/wp-admin/admin-ajax.php", method: 'post', data: { action: 'ajax_order', answer11: answer11, answer12: answer12, answer13: answer13 }, success: function (response) { $('.resp').html(response); } }); }); 

The function is a handler on the server, from which I want to take the data received from ajax until the response is sent (no matter in what form, array, string, etc.):

 function ajax_form(){ $answer11 = $_POST['answer11']; $answer12 = $_POST['answer12']; $answer13 = $_POST['answer13']; $response1 = $answer11 . $answer12 . $answer13; // как сохранить или передать это значение другой функции в файле function.php ? if ( defined( 'DOING_AJAX' ) && DOING_AJAX ){ echo $response1; // собственно все ок, ответ от сервера уходит wp_die(); } } add_action('wp_ajax_nopriv_ajax_order', 'ajax_form' ); add_action('wp_ajax_ajax_order', 'ajax_form' ); 

I wanted to write the data that comes to the file (it is on the server), but the request works and the file is empty

 function ajax_form(){ $answer11 = $_POST['answer11']; $answer12 = $_POST['answer12']; $answer13 = $_POST['answer13']; $response1 = $answer11 . $answer12 . $answer13; $fp = fopen("one_q.txt", "r+"); fwrite($fp, $response1); fclose($fp);// Запись в файл if ( defined( 'DOING_AJAX' ) && DOING_AJAX ){ echo $response1; // собственно все ок, ответ от сервера уходит wp_die(); } 

}

  • one
    Why not just pass the value as a parameter to a function? Something like your_function($response1); - Pyramidhead
  • You need to call your function from ajax_form() . I do not understand what you want. - KAGG Design
  • Do not call a function, but take data from $ _POST that comes from AJAX - alexei
  • "Why not just pass the value as a parameter to a function? Something like your_function ($ response1);" - does not work like this, I tried it - alexei
  • @alexei, so you take these parameters in your function and everything will work. - Pyramidhead

0