My problem is the same as in other cases, but my code is fine, I compared with another and followed all the instructions and good practices, but it does not work. Here my code:
jQuery("#genera-pdf").click(function(e){
e.preventDefault();
var price1 = jQuery('input[type=text][id=price1]').val();
var price2 = jQuery('input[type=text][id=price2]').val();
jQuery.ajax({
url : '/wp-admin/admin-ajax.php',
type: 'POST',
data: {action: 'pdfAction', a: price1, b: price2}),
success: function(data){
alert(data);
},
error: function(errorThrown){
alert(errorThrown);
}
});
});
And my function is:
function pdfAction(){
$post = $_POST['pdfAction'];
echo "something";
wp_die();
}
add_action('wp_ajax_pdfAction','pdfAction_callback');
add_action('wp_ajax_nopriv_pdfAction','pdfAction_callback');
Any idea that is happening?
My problem is the same as in other cases, but my code is fine, I compared with another and followed all the instructions and good practices, but it does not work. Here my code:
jQuery("#genera-pdf").click(function(e){
e.preventDefault();
var price1 = jQuery('input[type=text][id=price1]').val();
var price2 = jQuery('input[type=text][id=price2]').val();
jQuery.ajax({
url : '/wp-admin/admin-ajax.php',
type: 'POST',
data: {action: 'pdfAction', a: price1, b: price2}),
success: function(data){
alert(data);
},
error: function(errorThrown){
alert(errorThrown);
}
});
});
And my function is:
function pdfAction(){
$post = $_POST['pdfAction'];
echo "something";
wp_die();
}
add_action('wp_ajax_pdfAction','pdfAction_callback');
add_action('wp_ajax_nopriv_pdfAction','pdfAction_callback');
Any idea that is happening?
Share Improve this question asked Jun 11, 2019 at 14:48 Mirquez RodriguezMirquez Rodriguez 31 bronze badge 2 |1 Answer
Reset to default 2You function name and your ajax action callback name should be same.
add_action('wp_ajax_pdfAction','pdfAction_callback');
add_action('wp_ajax_nopriv_pdfAction','pdfAction_callback');
function pdfAction_callback(){
$post = $_POST['pdfAction'];
echo "something";
wp_die();
}
Please update your php code with the above.
Currently, your php function is not getting executed.
Update:
The Ajax Script,
$(document).on('click', "#generate-pdf", function(){
var price1 = $("#price1").val();
var price2 = $("#price2").val();
$.ajax({
url: ajax_object.ajax_url, // AJAX handler
data: {
'action': 'pdfAction',
'price_1': price1,
'price_2': price2
},
type: 'POST',
success: function(data) {
console.log(data);
console.log(data.price_1) // to access individual values
},
error: function(errorThrown){
console.log(errorThrown);
}
});
return false;
});
Register Ajax Action Url and Script,
function ajax_enqueue_scripts() {
// Enqueue your javascript file
wp_register_script( 'ajax-scripts', get_template_directory_uri() . '/js/ajax-scripts.js', array('jquery'), '1.0.0', true );
// in JavaScript, object properties are accessed as ajax_object.ajax_url
wp_localize_script( 'ajax-scripts', 'ajax_object', array(
'ajax_url' => admin_url( 'admin-ajax.php' ), // admin ajax url
));
wp_enqueue_script('ajax-scripts');
}
add_action( 'wp_enqueue_scripts', 'ajax_enqueue_scripts' );
Ajax Request Callback function,
add_action('wp_ajax_pdfAction','pdfAction_callback');
add_action('wp_ajax_nopriv_pdfAction','pdfAction_callback');
function pdfAction_callback(){
// Getting data sent from Ajax request
$price_1 = $_POST['price_1'];
$price_2 = $_POST['price_2'];
// Prepare Data to be send back to AJAX request
$return = array(
'price_1' => $price_1,
'price_2' => $price_2
);
wp_send_json($return); // Send JSON response back to our AJAX Request and die().
// we don't require wp_die(); here wp_send_json() will take care of it.
}
You also should consider using nonce here for security purpose.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745412819a4626608.html
0
with no explanation – Tom J Nowell ♦ Commented Jun 11, 2019 at 15:06