这可能是一个重复的问题,但我无法使用此链接”POST from jQuery to PHP“所以请帮助我实现这一点。我正在尝试将jquery变量传递给php函数并运行它。我有freshdeskreply.js文件,我的php函数位于主题文件夹的send\\u reply.php中。
在js文件中,我试图传递这个。name(即票证id)和textreply(即textarea值)到我的php函数并运行它。我已经试了一周了,但没能成功,请一步一步地给我看看。非常感谢。
我还能够提醒js值,但无法将其发布到php函数。js文件已注册并可用于页面加载。
功能。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();
}
});
});
给你回信。phpadd_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*/