user3788671 user3788671 - 1 year ago 102
Ajax Question

How to make an ajax call to a php function with parameters in functions.php file Wordpress

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()

echo $output;



And this is my ajax call:

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

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?

Answer Source

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