0

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.

functions.php

/*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*/

freshdeskreply.js

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

});

send_reply.php

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

    /*Reply*/
    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) {

                    }
                                    curl_close($ch);
    }
    /*End Reply*/
15
  • 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 Commented Jan 23, 2018 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? Commented Jan 23, 2018 at 21:51
  • Yes I added that hook but still no luck, I can not see where is the problem at Commented Jan 24, 2018 at 22:24
  • I edited my current code, above is the most updated one. Commented Jan 24, 2018 at 22:42
  • I think it doesn't run the php function Commented Jan 25, 2018 at 14:55

1 Answer 1

0

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

5
  • 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 Commented Jan 25, 2018 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. Commented Jan 25, 2018 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 Commented Jan 25, 2018 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. Commented Jan 25, 2018 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 Commented Jan 25, 2018 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.