0

In reading I am not sure if the issue below could have something to do with needing a custom wordpress ajax handler? All of these functions are inside my functions.php file.

I have a couple of PHP functions that are supposed to pass information from one to another. The an AJAX call to the final function should produce the final result through that call. Its not happening. My count results in 0 and I am not understanding why.

If I replace my query with a static array like [44,55,99,33] then it will pass the values ok. Likewise if I keep the query and simply replace the $id in the query and make it a static id such as '20' then it works. I would think that the id was the issue then and it wasnt pulling properly but it seems to work ok just not pass?

function idx(){
    global $wpdb, $id;
    
 $getId = $_SERVER['REQUEST_URI'];
    $ecp = explode('/', $getId);
    if($ecp[3] != ''){
        $id = $ecp[3];
    }    
    return $id;
}
add_action( 'after_setup_theme', 'idx' );

===============================================

function Total_Result(){
    global $wpdb;

   $id = idx();

      /*$resultx = [44,88,59,52];*/ ;

    $resultx = $wpdb->get_results( $wpdb->prepare("SELECT * FROM {$wpdb->prefix}custom_form_submission_conversation as cc LEFT JOIN {$wpdb->prefix}custom_form_submission as cs on cc.fsid ='$id' WHERE fsid='$id'" ));
    
    $resultxs = array($resultx);
    
    $total_result =  count($resultxs[0]);
    echo  $total_result;
}

when I var_dump $resultx I get:

array(3) { [0]=> object(stdClass)#1256 (36) { [“cid”]=> string(3) “167” [“fsid”]=> string(2) “20” [“specified_date”]=> string(3) “yes” [“price_quote”]=> string(4) “1140” [“posted_by”]=> string(9) “dc_vendor” [“limit_count”]=> string(1) “1” [“read_unread”]=> string(1) “1” [“vendor_response”]=> string(4) “zero” [“date_created”]=> string(19) “2021-04-15 11:39:08” } 

[1]=> object(stdClass)#1257 (36) { [“cid”]=> string(3) “186” [“fsid”]=> string(2) “20” [“specified_date”]=> string(3) “Yes” [“price_quote”]=> string(3) “602” [“posted_by”]=> string(8) “customer” [“limit_count”]=> string(1) “1” [“read_unread”]=> string(1) “1” [“vendor_response”]=> string(4) “zero” [“date_created”]=> string(19) “2021-04-15 11:39:08” } 

[2]=> object(stdClass)#1258 (36) { [“cid”]=> string(3) “188” [“fsid”]=> string(2) “20” [“specified_date”]=> string(3) “yes” [“price_quote”]=> string(4) “1017” [“posted_by”]=> string(9) “dc_vendor” [“limit_count”]=> string(1) “1” [“read_unread”]=> string(1) “0” [“vendor_response”]=> string(4) “zero” [“date_created”]=> string(19) “2021-04-15 11:39:08” } } 

so then I try and pass that data like so:

function getvendorresponsestatus() {
    global $wpdb;
$id = idx();
    
    $total_resultx = Total_Result($id);
        die;
}

Ive tried with and without the die();.

The AJAX call is:

setInterval(function(){
    var ffiidd = jQuery('#ffiidd').val();
    var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";     
    jQuery.post(ajaxurl, {
        action: "vendorAjaxprog",
        ffiidd: ffiidd
    }, function(data) {
        console.log(data);
        jQuery.post(ajaxurl, {
            action: "getvendorresponsestatus", //<<<<<<<< this is the action that calls the function
            fsid : ffiidd
        },
        function(datas) {
            console.log('+ datas);
            jQuery('.cust_vendor_top').html(datas);
        });
        if(data == 0){}else{
            jQuery('.ajaxreload').html(data);               
            
        }
    });         
    
}, 5000);/*==30 seconds==*/

So the question is why wont it pass along the query array information but it passes a basic array or it will pass if I change the id in the query to be static? I know the query is good because of when I var_dumped it, I have results before I get to the Total_Result() function.

3
  • global variables do not carry over from one request to another, that is not the purpose of the global keyword. If you want things to persist, you have to store them in the database/cookies. You can't store a value in a variable in one request, then use it in another. This is true of all requests, wether they are AJAX requests, page requests, XMLRPC or REST API requests. It's a fundamental part of how PHP works – Tom J Nowell Apr 16 at 22:52
  • In this case, it fails because you get the value of $id based on data in the URL, but that data is not there in the AJAX request. Since each request is loaded from a blank slate, previous versions of $id can't be used – Tom J Nowell Apr 16 at 22:59
  • thank you @tomjnowell – Manny Apr 17 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.