0

jquery.js?ver=1.12.4-wp:4

POST https://xyz/update.php 500 (Internal Server Error)

send @ jquery.js?ver=1.12.4-wp:4

ajax @ jquery.js?ver=1.12.4-wp:4 myFunction @ 6011c7fbf.min.js?ver=1600216310:3 onclick @ (index):453

I am getting a 500 error above from the console. What I dont know is whether the error is in my PHP in trying to update the row or elsewhere.

PHP below is contained inside my update-file.php file

function function_1() {
    global $wpdb;
$wpdb->query( $wpdb->prepare("UPDATE 'my_table_name' SET `currentstatus` = 'myupdate1' WHERE ID = '1'"));   

}

JAVASCRIPT contained on the page

function myFunction() {

      jQuery.ajax({
    type: 'post',
    url: '/wp-content/themes/yummy/update-file.php',
    success: function(data){
        // callback function
    }
});
  alert("I've been clicked!!");
}

HTML

Go!

EDIT 1

As per suggestions I have updated as such:

JAVASCIPT

jQuery.ajax({
    type: 'post',
    url: my_ajax.ajax_url,
    action: 'function_1',
    success: function(data){
        // callback function
    }
});

Thinking the above was not correct I also tried :

jQuery.ajax({
    type: 'post',
    url: my_ajax.https://myurl.com/wp-content/themes/yummy/update-waitinglist.php, // this is the location of the update php below
    action: 'function_1',
    success: function(data){
        // callback function
    }
});

PHP below is contained inside my update-file.php file

add_action('wp_ajax_function_1', 'myfunctionname'); // logged in user can make a call
add_action('wp_ajax_nopriv_function_1', 'myfunctionname'); // non logged in user can make a call

function myfunctionname() {
    global $wpdb;
    $results = $wpdb->query( $wpdb->prepare("UPDATE 'my_table_name' SET `currentstatus` = 'myupdate1' WHERE ID = '1'"));
    die($results);

}

ADDED TO FUNCTIONS FILE wp_localize_script('myfunctionname', 'my_ajax', array('ajax_url' => admin_url('admin-ajax.php')));

With the EDIT 1 in place, I also get an error - Notice: wp_localize_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. This notice was triggered by the wait list_update handle. Please see Debugging in WordPress for more information. www.xyz.com/wp-includes/functions.php on line 5225 . I must have misunderstood something in the suggestion.

EDIT 2 Got everything else working but the button doesnt seem to update anything.

PHP from functions file -

function my_scripts() {   
wp_enqueue_script( 'waitlist_update_call', get_template_directory_uri().'/assets/js/waitlist_update_call.js', array('jquery'), null, true );
wp_localize_script('waitlist_update_call', 'my_ajax', array('ajax_url' => admin_url('admin-ajax.php')));
    echo getcwd();
//calls Waitinglist data and creates table
}
add_action('wp_enqueue_scripts', 'my_scripts');

add_action('wp_ajax_waitlist_update_function', 'waitlist_update_function'); // logged in user can make a call

function waitlist_update_function() {
    global $wpdb;
    $results = $wpdb->query( $wpdb->prepare("UPDATE 'mytablename' SET `currentstatus` = 
    'myupdate1' WHERE ID = '1'"));
    die($results);

}

JS

// JavaScript Document
function update() {
jQuery.ajax({
    type: 'post',
    url: my_ajax.ajax_url,
    // add your action inside data object
    data: { 
      action: 'waitlist_update_function' 
    },
    success: function(data){
        // callback function
    }
});
}

HTML Go!

theodore
  • 49
  • 6

1 Answers1

0

The issue should be with the URL, it must be absolute I think.

jQuery.ajax({
    //....
    url: 'http://yourwebsite.com/wp-content/themes/yummy/update-waitlist.php'
    // ...

The WordPress way

You must enqueue your JS file script.js before with same handle and then localize after it

Localize the script to pass generic data. We will pass the ajax_url with my_ajax object.

functions.php

wp_localize_script('your-script-handle', 'my_ajax', array('ajax_url' => admin_url('admin-ajax.php')));

Then in your script file, you can use the my_ajax object to get the AJAX URL. Define a action function_1 which will be executed when this AJAX call is requested.

script.js

jQuery.ajax({
    type: 'post',
    url: my_ajax.ajax_url,
    data: {
      action: 'function_1',
    }
    success: function(data){
        // callback function
    }
});

Define a function and attach it with Ajax action that will query the database & returns the result.

functions.php

add_action('wp_ajax_function_1', 'function_to_execute_some_query'); // logged in user can make a call
add_action('wp_ajax_nopriv_function_1', 'function_to_execute_some_query'); // non logged in user can make a call

function function_to_execute_some_query() {
    global $wpdb;
    $results = $wpdb->query( $wpdb->prepare("UPDATE 'wp_wpdatatable_4' SET `currentstatus` = 
    'myupdate1' WHERE wdt_ID = '1'"));
    die($results);

}
Ashish Yadav
  • 1,901
  • 2
  • 17
  • 23
  • thanks so much for this detailed answer. I must be misinterpreting something because having followed the instructions above ( I think) I am getting a Uncaught ReferenceError: my_ajax is not defined on page load and a Uncaught ReferenceError: myFunction is not defined at HTMLButtonElement.onclick – theodore Sep 16 '20 at 02:00
  • also added above an additional error about scripts. – theodore Sep 16 '20 at 02:12
  • @theodore In your updated script, remove the `my_ajax.` and include the whole path of the file only inside single quotes. As I said in above answer, to use the generic data we need to localize it for the specific script & without localization it will not work. So, remove `my_ajax` and include the absolute path only. See the first part of my answer. Either follow the first answer or update everything with the answer below The WordPress way. – Ashish Yadav Sep 16 '20 at 03:30
  • Ah thanks. I will try and report back. I had taken it literally since someone above said dont use the whole file path. Confirming it would look like url: https://maydomain.com/path instead of my_ajax.ajax_url – theodore Sep 16 '20 at 03:43
  • Yes, it would look like the the URL you have mentioned & also you shouldn't use the whole path. You should follow the WordPress way which is recommended. – Ashish Yadav Sep 16 '20 at 04:41
  • Wait sorry now I am confused again. Do I use the whole path or no? I am using the wordpress method you so kindly wrote out – theodore Sep 16 '20 at 12:16
  • Sorry for the confusion :D , using the whole path without localizing is not recommended. So, follow the WordPress way, it should be all good. – Ashish Yadav Sep 16 '20 at 13:08
  • Still think I am missing something. To keep this cleaner, I started a new question for this issue https://stackoverflow.com/questions/63930544/wp-enqueue-script-order-and-localize-errors-hook-needed – theodore Sep 17 '20 at 03:27
  • Thanks for all the help. Dont suppose you could direct me on using the onclick to call the ajax command? I think I have it right since I am not getting errors but it is not actually doing anything either. I click and nothing happens. No errors – theodore Sep 20 '20 at 19:39
  • You have to wrap the Ajax code inside a function and attach the function with button's click event. – Ashish Yadav Sep 21 '20 at 00:29