django django - 6 months ago 21
Ajax Question

how to send parameter in ajax call

I have one AJAX call. I need to pass some parameters to my function. This is my AJAX call.

$.ajax({
url: 'lib/function.php',
data: {
action: 'getStoreSupplyItems',
id: store_id,
indent: 1
},
success: function(output) {
//alert(output);
$('#response').html(output);
}
});


Here is my back end function definition that I am trying to call:

function getStoreSupplyItems($category = '')
{
global $db;
$data = $_REQUEST;
$category = (!empty($category) ? ' AND cim.item_group_code IN ("'.$category.'") ' : '');

if ($data['id'] != "")
{
$store = $data['id'];
}
else
{
$store = $_SESSION['user']['store']['id'];
}


How can I pass some arguments to the function? The parameter that I want to pass is something like '12,5,6'.

Answer

You can put a switch statement in your php file and then call the function passing the arguments. Like this

switch($_REQUEST["action"]){
  case "getStoreSupplyItems" :
    getStoreSupplyItems("2,12,5");
    break;
}

In your getStoreSupplyItems function you can get the value as a param and also use other get params by $_REQUEST or $_GET.

I hope this is what you looking for.