I sent an ajax request by a button, ajax requests work perfectly also responding. I checked the browser Network it is fine. My problem is I am not getting value by PHP request. Here is my code given below:
function my_enqueue(){
wp_enqueue_script( 'ajax-script', plugin_dir_url( __FILE__ ) . '/assets/js/awqvCustom.js', array('jquery') );
wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue' );
/* Custom Ajax */
function data_custom_ajax(){
if ( isset($_POST)) {
$id = $_POST['id'];
echo $id;
}
die();
}
add_action('wp_ajax_nopriv_data_custom_ajax', 'data_custom_ajax');
add_action('wp_ajax_data_custom_ajax', 'data_custom_ajax');
/* Modal Markup*/
function modalBody(){?>
<div class="my-modal">
<div id="ajax_result"></div> //getting ajax success value here
<?php
$getId = (isset($_POST['id']) ? $_POST['id'] : '');
echo $getId; //nothing get
?>
</div>
<?php }
add_action('get_footer', 'modalBody');
jQuery ajax code:
jQuery(document).ready(function () {
'use strict';
jQuery(".btn-container").on('click','.open-modal', function(e){
e.preventDefault();
var id = jQuery(this).data('id');
jQuery.ajax({
type: "POST",
url: my_ajax_object.ajax_url,
dataType:"json",
data: {
action: 'data_custom_ajax',
id: id,
},
cache: false,
success: function(data){
if(data !=''){
jQuery('#ajax_result').html(data);
var modal = jQuery(".my-modal").wgModal();
modal.openModal();
}
}
});
});
I checked with the debugger. The ajax variables seem to be sent
Related
add_action( 'wp_footer', function () { ?>
<script type='text/javascript' id='ajax'>
const ajaxUrl = "<?php echo admin_url('admin-ajax.php'); ?>";
async function httpRequest(url = '', data = {}, method = 'POST') {
const response = await fetch(url, { method: method,
credentials: 'same-origin',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cache-Control': 'no-cache',
},
body: new URLSearchParams(data)
});
return response.text();
}
document.querySelector('#roll-title-btn').addEventListener('click', async function() {
const searchTerm = document.getElementById('roll-title');
if (searchTerm.value.trim().length) {
const result = await httpRequest(ajaxUrl, { action: 'filter_ajax', 'roll-title': searchTerm.value });
console.log(result);
}
})
</script>
<?php } );
add_action( 'wp_ajax_nopriv_filter_ajax', 'filter_ajax' );
add_action( 'wp_ajax_filter_ajax', 'filter_ajax' );
function filter_ajax() {
echo "TESTING";
wp_die();
}
Whenever I am trying to send an HTTP request it is throwing 400 bad requests and prints 0 as result. Trying to implement a filter plugin but the above code snippets aren't working at all. I have tried multiple solutions from StackOverflow but none worked.
Here I am sharing my working code which I was developed by today so you can check here.
//First I enqueue script using action in function.php file
wp_enqueue_script( 'custom-js', get_stylesheet_directory_uri().'/assets/js/custom.js', array(), '1.0.0', 'true' );
//Then I localize custom js for admin ajax
wp_localize_script( 'custom-js', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
//I choose action and javascript object "my_ajax_object"
//Now I create custom.js as per mentioned above path
$(function(){
$('#capture').click(function(){
//capture is button id.
//here is the ajax call
var value = '123';
jQuery.ajax({
type: "post",
dataType: "HTML",
url: my_ajax_object.ajax_url,
data : {action: "my_ajax_object","value":value},
success: function(msg){
console.log(msg);
}
});
});
});
//Now I wrote ajax function
add_action( 'wp_ajax_nopriv_my_ajax_object', 'my_ajax_object' );
add_action( 'wp_ajax_my_ajax_object', 'my_ajax_object' );
function my_ajax_object()
{
$value = $_POST['value'];
$response = '<p>value is '.$value.'<p>';
return $response;
wp_die();
}
Im having an issue with calling ajax from front of my wordpress site The data just dose not go through to the other file when the ajax is called on change. The value in ajax this.value is passed ok from .
<?php
function my_enqueue()
{
wp_enqueue_script('ajax-script', get_template_directory_uri() . '/js/my-ajax-script.js', array('jquery'));
wp_localize_script('ajax-script', 'my_ajax_object', array('ajax_url' => admin_url('admin-ajax.php')));
}
add_action('wp_enqueue_scripts', 'my_enqueue');
....
<script>
jQuery('#courseid').on('change', function() {
jQuery.ajax({
type: 'POST',
url: '<?= get_template_directory_uri(); ?>/inc/ssc/public_ajax_options_topics.php',
data: {
action: 'g6_options_topics',
product_id: this.value
}
}).done(function(data) {
jQuery("#topicid").html(data);
});
});
</script>
This is the file that response is send to
<?php
require_once('../../../../../wp-load.php');
$mysqli = db();
$product_id = 0;
if ($_POST['product_id']) {
$product_id = $_POST['product_id'];
}
I using ACF wordpress to update acf field for user to populate with button id click.
I am getting Error as admin-ajax.php with 400
Error POST https://domainname/wp-admin/admin-ajax.php 400 ()
Wordpress code on template
<button class="get_project" id="1479" name="urbtnid" data-postid="147">Get project</button>
<button class="get_userbtn" value="<?php echo get_current_user_id() ?>" id="get_userbtn" ><?php echo get_current_user_id() ?></button>
**
JQUery AJAX Code
**
jQuery(function($){
$('.get_project').click(function() {
var stdid = jQuery('#get_userbtn').attr('value');
var stdbtnid = this.id;
jQuery.ajax({
url: my_ajax_object.ajax_url,
type : 'post',
data: dataString,
success : function( response ) {
console.log(response);
}
});
})
});
**
Function.php code
**
function my_enqueue_ajax_save() {
wp_register_script( 'ajax-script', get_template_directory_uri() . '/pradeep-js-css/my-ajax-id-save-script.js', array('jquery') , false, true );
wp_enqueue_script( 'ajax-script' );
wp_localize_script( 'ajax-script', 'my_ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_ajax_save' );
add_action( 'wp_ajax_my_action', 'my_action' );
add_action( 'wp_ajax_nopriv_my_action', 'my_action' );
function my_action(){
$postid = $_POST['stdid1'];;
$fieldname = 'last_question_viewed';
$fieldvalue = $_POST['stdbtnid1'];
update_post_meta($postid, $fieldname, $fieldvalue);
echo $postid;
wp_die($fieldname = '', $fieldvalue = '', $postid = '');
}
Updated jquery with action
jQuery(function($){
var stdid = jQuery('#get_userbtn').attr('value');
var stdbtnid = this.id;
var dataString = 'stdid1=' + stdid + '&stdbtnid1=' + stdbtnid;
$('.get_project').click(function() {
var data = {
action: "my_action",
stdid: stdid,
stdbtnid1: stdbtnid
};
jQuery.ajax({
url: my_ajax_object.ajax_url,
type : 'post',
data: data,
success : function( response ) {
console.log(response);
}
});
})
});
I made a wordpress ajax call code in which when i click on a button the the function runs properly and returns the value first time as it should but after that the ajax call to admin-ajax.php keepon running as i can see from developer tools . My code is
add_action( 'wp_ajax_arete_bp_endorse', 'arete_bp_endorse_ajax' );
add_action( 'wp_ajax_nopriv_arete_bp_endorse', 'arete_bp_endorse_ajax' );
function arete_bp_endorse_ajax(){
global $wpdb;
$result['type'] = "works";
echo $result;
wp_die();
}
function my_script_enqueuer() {
wp_register_script( "my_voter_script", WP_PLUGIN_URL.'/example/js/example.js', array('jquery') );
wp_localize_script( 'my_voter_script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'my_voter_script' );
}
the example.js file code
jQuery(document).on("click",".endorse-bt", function (event){
event.preventDefault();
var type= jQuery(this).attr("data");
var option= jQuery(this).parents("li").attr("data-endorsed-item-name");
var profile_id=jQuery(this).parents("li").attr("data-prf-id");
var login_id=jQuery(this).parents("li").attr("data-lg-id");
jQuery.ajax({
dataType : "json",
url : myAjax.ajaxurl,
type: 'POST',
data:{action: "arete_bp_endorse", type : type, option: option, profile_id: profile_id, login_id: login_id},
success: function( response ){
//Do something with the result from server
alert(response.type);
}
});
});
I want to send an AJAX request in WordPress which tracks my clicks. So far, I have added this in my functions file:
add_action('init', 'my_script_enqueuer');
function my_script_enqueuer() {
wp_register_script("history_script", get_template_directory_uri() . '/js/history_script.js', array('jquery'));
wp_localize_script('history_script', 'myAjax', array('ajaxurl' => get_template_directory_uri().'/functions.php'));
wp_enqueue_script('jquery');
wp_enqueue_script('history_script');
}
add_action("wp_ajax_history_trace", "history_trace");
function history_trace() {
echo 'fasfasgasgas'; die;
}
And this in my js file :
jQuery(document).ready( function() {
jQuery("#searchsubmit").click( function() {
jQuery.ajax({
type : "post",
dataType : "json",
url : myAjax.ajaxurl,
data : {action: "history_trace"},
success: function(response) {
if(response.type == "success") {
alert('success')
}
else {
alert("false")
}
}
})
})
})
But in my console, the request appears in red, and there is no response. Please Help!
In WordPress, use the following url to process AJAX requests:
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) )
It's also important to localize a script after it has been enqueued.