2

I am trying to make a call to a php function that I have added to my functions.php file in WordPress. I am able to execute the code without parameters but not sure how modify my function and ajax call to accept my posted data.

Within the functions.php file in WordPress this my code:

add_action('wp_ajax_get_parts', 'get_parts');
add_action('wp_ajax_nopriv_get_parts', 'get_parts');

function get_parts()
{
       //DOING STUFF

        echo $output;

        wp_reset_postdata();

        die();
}

And this is my ajax call:

$.post("/wp-admin/admin-ajax.php", {'action':'get_parts'}, function(data, status){
    //DOING STUFF...
});

I want to change my php function to accept two parameters (partnumber and zipcode):

function get_parts($partnumber, $zipcode)

And within my ajax call I would like to pass two values from textboxes (Example of what i have):

$.post("/wp-admin/admin-ajax.php", {'action':'get_parts', 'partnumber' : $("#searchtbx").val(), 'zipcode' : $("#ziptbx").val()},
function(data, status){

});

As of right now I am able to call the function and get data. How do I pass two parameters (partnumber and zipcode) to php function?

1 Answer 1

2

You can access them, but not as the function parameters. They are in $_POST. So, $_POST['partnumber'] and $_POST['zipcode'].

Sign up to request clarification or add additional context in comments.

3 Comments

I have tried using $_POST['partnumber'] and it doesnt appear that I am getting any data back. I have used fiddler to confirm that the data is being passed and it is. Is there anything else I have to add?
This is how I am using the data:$url = "pacificdda.com/ppgapi/api/partslocatorparts?partnumber=" . $_POST['partnumber'] . "&zipcode=" . $_POST['zipcode']; If i hardcode strings into the parameters it works fine.
Nevermind, I had to convert them to strings. thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.