This is possible duplicate question but I couldn't make it work with this link "POST from jQuery to PHP" So please help me to achieve this. I am trying to pass jquery variables to php function and run it. I have freshdeskreply.js file and I have my php function in send_reply.php in theme folder.

On js file I am trying to pass this.name (which is ticket id) and textreply (which is textarea value) to my php function and run it. I've been trying to do this for a week already but couldn't make it work, please show it to me step by step. Thank you.

I am also able to alert the js values but can not post them to php function. js file is already registered and works on page load.


/*Reply JS*/
add_action( 'wp_enqueue_scripts', 'my_scripts' );

function my_scripts() {
    wp_enqueue_script( 'my_js_library', get_stylesheet_directory_uri() . '/freshdeskreply.js' );
    wp_localize_script( 'my_js_library', 'my_local_var', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
/*END - Reply JS*/


jQuery(document).ready(function($) {   
         /*Ticket Reply button*/
      $('[id^="send_reply"]').click(function() {
         var textreply = $('textarea#replyarea' + this.name).val();
       alert("Reply area can not be empty...");
    url: my_local_var.ajax_url, 
    type: 'POST',
      action: 'myaction', 
      textreply: textreply,
      ticketid: this.name
    success: function( data ){
      //Do something with the result from server
      console.log( data );



add_action( 'wp_ajax_myaction', 'post_note' );
add_action( 'wp_ajax_nopriv_myaction', 'post_note' );

    function post_note() {

                    $api_key = "XXXXXXXXXXXXXXX";
                    $password = "x";
                    $yourdomain = "XXX";

                    $ticketid = $_POST['ticketid'];
                    $reply_body = $_POST['textreply'];

                    $notprivate = "false";

                    $note_payload = array(
                      'body' => $reply_body,
                      'private' => $notprivate,
                      'user_id' => $contact_id,

                    $url = "https://$yourdomain.freshdesk.com/api/v2/tickets/$ticketid/notes";
                    $ch = curl_init($url);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                    curl_setopt($ch, CURLOPT_POST, true);
                    curl_setopt($ch, CURLOPT_HEADER, true);
                    curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$password");
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $note_payload);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                    $server_output = curl_exec($ch);
                    $info = curl_getinfo($ch);
                    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
                    $headers = substr($server_output, 0, $header_size);
                    $response = substr($server_output, $header_size);

                    if($info['http_code'] == 201) {

    /*End Reply*/
  • 4
    It means refactoring what you've got, but after a week I would recommend investing the time in understanding how WordPress handles Ajax natively. The benefits far outweigh the drawbacks . premium.wpmudev.org/blog/using-ajax-with-wordpress – jdm2112 Jan 23 '18 at 21:30
  • Thank you for the link, I have already found this one too but still couldn't make it. How am I going to call that specific function? – C.Yasa Jan 23 '18 at 21:51
  • Yes I added that hook but still no luck, I can not see where is the problem at – C.Yasa Jan 24 '18 at 22:24
  • I edited my current code, above is the most updated one. – C.Yasa Jan 24 '18 at 22:42
  • I think it doesn't run the php function – C.Yasa Jan 25 '18 at 14:55

Tested this locally and found the issues.

When you reg/enq the JS, you need to localize the script. This is your chance to pass anything from PHP to the script. In this case, you should, at minimum, pass the WP ajax url like this:

    wp_localize_script( 'my_js_library', 'my_local_var', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );

More details here: https://codex.wordpress.org/Function_Reference/wp_localize_script#Usage

In your JS, update the URL param given to the ajax() call to use this value:

url: 'send_reply.php', // wrong
url: my_local_var.ajax_url,  // right

You should also enqueue your script inside a function hooked to wp_enqueue_scripts. You may be doing this and simply didn't past above. Add this code to functions.php:

add_action( 'wp_enqueue_scripts', 'my_scripts' );

function my_scripts() {
    wp_enqueue_script( 'my_js_library', get_stylesheet_directory_uri() . '/freshdeskreply.js' );
    wp_localize_script( 'my_js_library', 'my_local_var', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );

NOTE: PHP will not be available for execution unless it has been included (to functions.php, etc) or hooked to an action. The OP's callback and action hooks were in an external PHP file which was not loaded.

EDIT: fixed code formatting from original post.

EDIT: clarified where to add enqueue and localization code.

EDIT: added note to move ajax actions and callback to functions.php

  • When I added add_action( 'wp_enqueue_scripts', 'my_scripts' ); js doesn't work so, I added them wp_enqueue_script( 'my_js_library', get_stylesheet_directory_uri() . '/freshdeskreply.js' ); wp_localize_script( 'my_js_library', 'my_local_var', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); individually in support-template.php. However, still it doesn't call the send_reply.php – C.Yasa Jan 25 '18 at 16:56
  • Can you be more specific than "doesn't work"? It's hard to determine a solution from that. Update the code in question please and I will look at it. – jdm2112 Jan 25 '18 at 16:59
  • JS alerts didn't work. I have 3 files support-template.php (includes the button and js wp_enqueue_script( 'my_js_library', get_stylesheet_directory_uri() . '/freshdeskreply.js' );) - freshdeskreply.js (which takes the data from support-template.php) - send_reply.php (this is for to get the data from js and send it to freshdesk.) I also updated the code above – C.Yasa Jan 25 '18 at 17:04
  • Is your JS file loading on the page? You mention the enqueue statement was failing. Using the my_scripts() function above, hooked to wp_enqueue_scripts on my test machine enqueues the JS OK. – jdm2112 Jan 25 '18 at 17:10
  • Yes when I use add_action wp_enqueue_scripts hook js doesn't work, alerts not working. If I add it without the add_action then alerts work – C.Yasa Jan 25 '18 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.