WordPress admin ajax not working correctly












0















I have this PHP page in my WordPress plugin settings page:



<input type="button" class="button button-primary" onclick="update(<?php echo $id; ?>)" value="Done">


This is my JS function which gets called on button click:



function update(id) {

jQuery('.table100 .blockUI').removeClass('invisible');

var data = {
'action': 'update_table',
'id': id
};

var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";

jQuery.post(ajaxurl, data, function () {
}).success(function () {
location.reload();
}).fail(function (data) {
jQuery('.table100 .blockUI').addClass('invisible');
alert('An error occured during the database update!');
});
}


This is the function which should be executed when the js functions gets executed:



add_action( 'wp_ajax_update_table', 'update_table' );
function update_table() {
$id = $_POST['id'];
global $wpdb;

if ( $id > 0 && ! empty( $id ) ) {
$result = $wpdb->update( 'my_table', array( 'xyz' => '1' ), array( 'id' => $id ) );

if ( empty( $result ) ) {
wp_send_json_error( null, 400 );
wp_die();
}

} else {
header( 'HTTP/1.1 500 Internal Server Error' );
header( 'Content-Type: application/json; charset=UTF-8' );
die( json_encode( array(
'message' => 'ERROR. Id missing. Can not update my_table.',
'code' => 1337
) ) );
}
}


All three parts are at the same page and I've tried to trigger it but I'm always getting 400 Bad Request issue. I've added a debug to the PHP function to find out if the function gets called but no result.



My goal:
My goal is it to update a value in the database where id = submitted id.










share|improve this question























  • Chrome and logged in. As i know an access to the admin page is not possible when an admin user is not logged in.

    – Mr. Jo
    Nov 15 '18 at 18:37











  • Yeah, you're right. I re-read your question and you did mention that you're doing this on a custom plugin's settings page. I tried your code on a test plugin with a few minor modifications (stripped out some code, like your DB queries) and it worked for me just fine, though. No error 400.

    – cabrerahector
    Nov 15 '18 at 19:10








  • 1





    Is it possible that the row in your DB is already correct? From the WP docs: 'Keep in mind that if the $data matches what is already in the database, no rows will be updated, so 0 will be returned. Because of this, you should probably check the return with false === $result'.

    – jh1711
    Nov 15 '18 at 19:16






  • 2





    So finally I found the problem. I can't add this function on the same page. I moved it to the functions.php and now it works.

    – Mr. Jo
    Nov 15 '18 at 23:13
















0















I have this PHP page in my WordPress plugin settings page:



<input type="button" class="button button-primary" onclick="update(<?php echo $id; ?>)" value="Done">


This is my JS function which gets called on button click:



function update(id) {

jQuery('.table100 .blockUI').removeClass('invisible');

var data = {
'action': 'update_table',
'id': id
};

var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";

jQuery.post(ajaxurl, data, function () {
}).success(function () {
location.reload();
}).fail(function (data) {
jQuery('.table100 .blockUI').addClass('invisible');
alert('An error occured during the database update!');
});
}


This is the function which should be executed when the js functions gets executed:



add_action( 'wp_ajax_update_table', 'update_table' );
function update_table() {
$id = $_POST['id'];
global $wpdb;

if ( $id > 0 && ! empty( $id ) ) {
$result = $wpdb->update( 'my_table', array( 'xyz' => '1' ), array( 'id' => $id ) );

if ( empty( $result ) ) {
wp_send_json_error( null, 400 );
wp_die();
}

} else {
header( 'HTTP/1.1 500 Internal Server Error' );
header( 'Content-Type: application/json; charset=UTF-8' );
die( json_encode( array(
'message' => 'ERROR. Id missing. Can not update my_table.',
'code' => 1337
) ) );
}
}


All three parts are at the same page and I've tried to trigger it but I'm always getting 400 Bad Request issue. I've added a debug to the PHP function to find out if the function gets called but no result.



My goal:
My goal is it to update a value in the database where id = submitted id.










share|improve this question























  • Chrome and logged in. As i know an access to the admin page is not possible when an admin user is not logged in.

    – Mr. Jo
    Nov 15 '18 at 18:37











  • Yeah, you're right. I re-read your question and you did mention that you're doing this on a custom plugin's settings page. I tried your code on a test plugin with a few minor modifications (stripped out some code, like your DB queries) and it worked for me just fine, though. No error 400.

    – cabrerahector
    Nov 15 '18 at 19:10








  • 1





    Is it possible that the row in your DB is already correct? From the WP docs: 'Keep in mind that if the $data matches what is already in the database, no rows will be updated, so 0 will be returned. Because of this, you should probably check the return with false === $result'.

    – jh1711
    Nov 15 '18 at 19:16






  • 2





    So finally I found the problem. I can't add this function on the same page. I moved it to the functions.php and now it works.

    – Mr. Jo
    Nov 15 '18 at 23:13














0












0








0








I have this PHP page in my WordPress plugin settings page:



<input type="button" class="button button-primary" onclick="update(<?php echo $id; ?>)" value="Done">


This is my JS function which gets called on button click:



function update(id) {

jQuery('.table100 .blockUI').removeClass('invisible');

var data = {
'action': 'update_table',
'id': id
};

var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";

jQuery.post(ajaxurl, data, function () {
}).success(function () {
location.reload();
}).fail(function (data) {
jQuery('.table100 .blockUI').addClass('invisible');
alert('An error occured during the database update!');
});
}


This is the function which should be executed when the js functions gets executed:



add_action( 'wp_ajax_update_table', 'update_table' );
function update_table() {
$id = $_POST['id'];
global $wpdb;

if ( $id > 0 && ! empty( $id ) ) {
$result = $wpdb->update( 'my_table', array( 'xyz' => '1' ), array( 'id' => $id ) );

if ( empty( $result ) ) {
wp_send_json_error( null, 400 );
wp_die();
}

} else {
header( 'HTTP/1.1 500 Internal Server Error' );
header( 'Content-Type: application/json; charset=UTF-8' );
die( json_encode( array(
'message' => 'ERROR. Id missing. Can not update my_table.',
'code' => 1337
) ) );
}
}


All three parts are at the same page and I've tried to trigger it but I'm always getting 400 Bad Request issue. I've added a debug to the PHP function to find out if the function gets called but no result.



My goal:
My goal is it to update a value in the database where id = submitted id.










share|improve this question














I have this PHP page in my WordPress plugin settings page:



<input type="button" class="button button-primary" onclick="update(<?php echo $id; ?>)" value="Done">


This is my JS function which gets called on button click:



function update(id) {

jQuery('.table100 .blockUI').removeClass('invisible');

var data = {
'action': 'update_table',
'id': id
};

var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";

jQuery.post(ajaxurl, data, function () {
}).success(function () {
location.reload();
}).fail(function (data) {
jQuery('.table100 .blockUI').addClass('invisible');
alert('An error occured during the database update!');
});
}


This is the function which should be executed when the js functions gets executed:



add_action( 'wp_ajax_update_table', 'update_table' );
function update_table() {
$id = $_POST['id'];
global $wpdb;

if ( $id > 0 && ! empty( $id ) ) {
$result = $wpdb->update( 'my_table', array( 'xyz' => '1' ), array( 'id' => $id ) );

if ( empty( $result ) ) {
wp_send_json_error( null, 400 );
wp_die();
}

} else {
header( 'HTTP/1.1 500 Internal Server Error' );
header( 'Content-Type: application/json; charset=UTF-8' );
die( json_encode( array(
'message' => 'ERROR. Id missing. Can not update my_table.',
'code' => 1337
) ) );
}
}


All three parts are at the same page and I've tried to trigger it but I'm always getting 400 Bad Request issue. I've added a debug to the PHP function to find out if the function gets called but no result.



My goal:
My goal is it to update a value in the database where id = submitted id.







php wordpress






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 18:27









Mr. JoMr. Jo

846218




846218













  • Chrome and logged in. As i know an access to the admin page is not possible when an admin user is not logged in.

    – Mr. Jo
    Nov 15 '18 at 18:37











  • Yeah, you're right. I re-read your question and you did mention that you're doing this on a custom plugin's settings page. I tried your code on a test plugin with a few minor modifications (stripped out some code, like your DB queries) and it worked for me just fine, though. No error 400.

    – cabrerahector
    Nov 15 '18 at 19:10








  • 1





    Is it possible that the row in your DB is already correct? From the WP docs: 'Keep in mind that if the $data matches what is already in the database, no rows will be updated, so 0 will be returned. Because of this, you should probably check the return with false === $result'.

    – jh1711
    Nov 15 '18 at 19:16






  • 2





    So finally I found the problem. I can't add this function on the same page. I moved it to the functions.php and now it works.

    – Mr. Jo
    Nov 15 '18 at 23:13



















  • Chrome and logged in. As i know an access to the admin page is not possible when an admin user is not logged in.

    – Mr. Jo
    Nov 15 '18 at 18:37











  • Yeah, you're right. I re-read your question and you did mention that you're doing this on a custom plugin's settings page. I tried your code on a test plugin with a few minor modifications (stripped out some code, like your DB queries) and it worked for me just fine, though. No error 400.

    – cabrerahector
    Nov 15 '18 at 19:10








  • 1





    Is it possible that the row in your DB is already correct? From the WP docs: 'Keep in mind that if the $data matches what is already in the database, no rows will be updated, so 0 will be returned. Because of this, you should probably check the return with false === $result'.

    – jh1711
    Nov 15 '18 at 19:16






  • 2





    So finally I found the problem. I can't add this function on the same page. I moved it to the functions.php and now it works.

    – Mr. Jo
    Nov 15 '18 at 23:13

















Chrome and logged in. As i know an access to the admin page is not possible when an admin user is not logged in.

– Mr. Jo
Nov 15 '18 at 18:37





Chrome and logged in. As i know an access to the admin page is not possible when an admin user is not logged in.

– Mr. Jo
Nov 15 '18 at 18:37













Yeah, you're right. I re-read your question and you did mention that you're doing this on a custom plugin's settings page. I tried your code on a test plugin with a few minor modifications (stripped out some code, like your DB queries) and it worked for me just fine, though. No error 400.

– cabrerahector
Nov 15 '18 at 19:10







Yeah, you're right. I re-read your question and you did mention that you're doing this on a custom plugin's settings page. I tried your code on a test plugin with a few minor modifications (stripped out some code, like your DB queries) and it worked for me just fine, though. No error 400.

– cabrerahector
Nov 15 '18 at 19:10






1




1





Is it possible that the row in your DB is already correct? From the WP docs: 'Keep in mind that if the $data matches what is already in the database, no rows will be updated, so 0 will be returned. Because of this, you should probably check the return with false === $result'.

– jh1711
Nov 15 '18 at 19:16





Is it possible that the row in your DB is already correct? From the WP docs: 'Keep in mind that if the $data matches what is already in the database, no rows will be updated, so 0 will be returned. Because of this, you should probably check the return with false === $result'.

– jh1711
Nov 15 '18 at 19:16




2




2





So finally I found the problem. I can't add this function on the same page. I moved it to the functions.php and now it works.

– Mr. Jo
Nov 15 '18 at 23:13





So finally I found the problem. I can't add this function on the same page. I moved it to the functions.php and now it works.

– Mr. Jo
Nov 15 '18 at 23:13












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325775%2fwordpress-admin-ajax-not-working-correctly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325775%2fwordpress-admin-ajax-not-working-correctly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python