POST mywebsite/wp-admin/admin-ajax.php 400 (Bad Request) - php

I am using ajax for handling filters in my custom post type but getting a Bad request on both POST & GET methods don't know why. Here is my Ajax code,
(function($) {
$(document).ready(function(){
$(document).on('submit', '[data-js-form=filter]', function(e){
e.preventDefault();
var data = $(this).serialize();
console.log(data);
var ajaxscript = { ajax_url : '//localhost/experiencecamping/rv-sales/wp-admin/admin-ajax.php' }
$.ajax({
url: ajaxscript.ajax_url,
type: "POST",
data: data,
success: function(result) {
console.log(result);
},
error: function(result) {
console.warn(result);
},
});
});
});
})(jQuery);
add_action('wp_ajax_nopriv_filter', 'filter_ajax');
add_action('wp_ajax_filter', 'filter_ajax');

For this wp_ajax request to work we need to specify action = filter in the data array we send via POST
Also we need filter_ajax() function
Maybe you have it here but you haven't copy it to your question.
Try this version of the code
(function($) {
$(document).ready(function(){
$(document).on('submit', '[data-js-form=filter]', function(e){
e.preventDefault();
var data = $(this).serialize();
console.log(data);
var ajaxscript = { ajax_url : '//localhost/experiencecamping/rv-sales/wp-admin/admin-ajax.php' }
$.ajax({
url: ajaxscript.ajax_url,
type: "POST",
data: {
query: 'test',
action: 'filter'
},
success: function(result) {
console.log(result);
},
error: function(result) {
console.warn(result);
},
});
});
});
})(jQuery);
add_action('wp_ajax_nopriv_filter', 'filter_ajax');
add_action('wp_ajax_filter', 'filter_ajax');
function filter_ajax(){
echo 321;
wp_die();
}

Related

Wordpress ajax request not working properly

I am new to wordpress, i make a ajax request on click button and it print the data, but ajax is not giving me any response. Please help me to find out the error.
Here is my code
add_action("wp_ajax_delivery_options", "delivery_options");
add_action("wp_ajax_nopriv_delivery_options", "delivery_options");
function delivery_options()
{
echo json_encode(array('type' => 'success'));
wp_die();
}
wp_enqueue_script("my-ajax-handle", get_stylesheet_directory_uri() . "/js/custom.js", array('jquery'));
wp_localize_script('my-ajax-handle', 'the_ajax_script', array('ajaxurl' => admin_url('admin-ajax.php')));
Ajax
(function($) {
$(document).ready(function() {
$('#delivery_option button').on('click', function(e) {
e.preventDefault();
var data = e.currentTarget.id;
$.ajax({
type: 'POST',
dataType: 'json',
url: the_ajax_script.ajaxurl,
data: { delivery_option: data },
success: function(response) {
console.log(response);
}
});
});
});
})(jQuery);
Any solution appreciated!
you have to pass "callback function name" in data: { action: 'delivery_options', delivery_option: data },
(function($) {
$(document).ready(function() {
$('#delivery_option button').on('click', function(e) {
e.preventDefault();
var data = e.currentTarget.id;
$.ajax({
type: 'POST',
dataType: 'json',
url: the_ajax_script.ajaxurl,
data: { action: 'delivery_options', delivery_option: data },
success: function(response) {
console.log(response);
}
});
});
});})(jQuery);

Ajax not passing by post method to php

Im using google chrome 65.0.3325.181 on windows 10, xampp is on to run the php. title explains the rest.
html/php:
$(document).ready(function (){
$('#sel_edificio').load('data.php');
$( ".form-control" ).change(function() {
var dato = 50;//document.getElementById("sel_edificio").value;
$.ajax({
method: "POST",
data: {'key': dato},
url: "uno.php",
success: function(status){
var asd = $('#test').load('uno.php');
//document.getElementById("NumEstudiantes").value(key);
}
});
});
});
</script>
uno.php:
<?php
echo $_POST['key'];
?>
error:
Notice: Undefined index: key in C:\xampp\htdocs\jqbd\uno.php on line 2
You are sending request two time, try this:
html/php
$(document).ready(function (){
$('#sel_edificio').load('data.php');
$( ".form-control" ).change(function() {
var dato = 50;//document.getElementById("sel_edificio").value;
$.ajax({
method: "POST",
data: {'key': dato},
url: "uno.php",
success: function(data){
$('#test').html(data);
}
});
});
});
change ajax method to type, try this
$.ajax({
type: "POST",
data: {'key': dato},
dataType: "json",
url: "uno.php",
success: function(status){
//var asd = $('#test').load('uno.php');
$('#test').load('uno.php', { key: dato });//document.getElementById("NumEstudiantes").value(key);
}
});

Jquery Ajax submitting empty textarea text to the database

I am trying to insert a textarea's text to my MySQL database, but my problem is when my program tries to submit empty text to the database.
When I remove e.preventDefault(); it is working just fine. What am I doing wrong?
var frm = $('#newsform');
frm.submit(function (e) {
e.preventDefault();
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
alert('Submission was successful.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
error: function (data) {
console.log('An error occurred.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
});
});
<form action="updatenews.php" method="POST" id="newsform">
<textarea name="news"></textarea>
<input type="submit">
</form>
var frm = $('#newsform');
frm.submit(function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: frm.attr('action'),
data: {'frm':frm},
success: function (data) {
alert('Submission was successful.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
error: function (data) {
console.log('An error occurred.');
console.log(data);
//clear all fields
$('#newsform').trigger("reset");
},
});
});

html link value retrieve by Js ajax send to php

I have a html link with a value inside like this.
<a data-toggle='modal' data-id='1' href='#myModal' class='marker' title='Edit'>Link</a>
I have a Js script that trigger a php that I want to send the value data-id
<script>
$(document).on("click", ".marker", function () {
var myBookId = $(this).data('id');
$.ajax({
type: "post",
url: "update.php", //
data: myBookId,
success: function(msg) {
$("#thanks").html(msg)
},
error: function() {
alert("failure");
}
});
});
</script>
And in my php I have this
if (isset($_POST['myBookId'])) {
$emp_id = strip_tags($_POST['myBookId']);
echo $emp_id;
But something is wrong the value is not pass.
Your problem is on the params of the AJAX call. Try this way:
data: { myBookId: myBookId },
try
$('.marker')click(function(){
var myBookId = $().attr('data-id');
$.ajax({
type: "post",
url: "update.php", //
data: myBookId,
success: function(msg){
$("#thanks").html(msg)
},
error: function(){
alert("failure");
}
});
return false;
});

Form Submition Ajax call in jquery mobile

I want to submit form throught ajax call in jquery mobile.
My script is that
<script>
function confirm(){
var user_name = $('#login_form').find('input[name="user_name"]').val();
$.ajax({
type: "POST",
url: $('#login_form').find('input[name="action"]').val(),
data: "val=" + user_name,
success: function(data){
alert(data);
}
});
}
</script>
My Form is here....
Name
Email
Password
Please it is urgent..........
function confirm(){
var frm = $('#login_form');
frm.submit(function () {
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
alert('ok');
}
error: function(xhr,err){
alert(err);
}
});
return false;
});
}
You can use this function to post your data...
You can try this function also...
$("#login_form").submit(function() {
$.ajax({
type: "POST",
url: $(this).attr('action'),
data: $("#login_form").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
error:function(xhr,err){
alert(err);
}
});
return false; // avoid to execute the actual submit of the form.
});

Categories