0

WordPress

I have a ajax function passing value to a function in functions.php The function on functions.php sends the response to ajax success but the value I am passing using ajax is not going to the function in functions.php

$.ajax({
        url: ajaxStuff.ajaxurl,
        type: "GET",
        processData :true,
        data: {
            action: 'filter_package',
            value: data,
        },
        
        success: function (data) {
            alert(data);
        },
    }); 

The console.log for data shows as:

{cat_names: Array(1), reg_names: Array(0), dest_names: Array(0), fromPrice: '', toPrice: '', …}

which is correct one.

If I enter some hardcoded message to my filter_package function in functions.php the ajax shows the response which means the ajax function hits my function on functions.php but when I try to get the data from ajax on my function I shows Array0

function filter_package()
{

    $value = $_REQUEST['value'];    
    $cat_slug = $value['cat_names'];
    echo 'sadasd'.$cat_slug;

The output:

sadasdArray0
risav55
  • 35
  • 8

1 Answers1

0

check your ajax url and make sure admin.ajax hit correctly in networkbdev tools

also can check Ajax call to PHP action/function with array as data (in wordpress)

  • I also have a contact form which sends mail to client using as like same ajax function and it works perfectly. – risav55 Aug 28 '22 at 01:31
  • It seems my admin-ajax is returning 0 for this problem and is ok with contact form – risav55 Aug 28 '22 at 01:47
  • `sadasdArray0` in your output you have `Array` that comes from `$cat_slug` so everything is fine here. You just trying to echo an array, you better use `print_r` or `var_dump` function. – Vijay Hardaha Aug 28 '22 at 13:11
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 02 '22 at 06:33