2016-06-08 22 views
-1

我有一個AJAX調用。我需要傳遞一些參數給我的函數。這是我的AJAX調用。如何在ajax調用中發送參數

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

這裏是我的後端函數的定義,我想呼籲:

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']; 
    } 

我怎麼能傳遞一些參數的功能?我想要傳遞的參數就像'12,5,6'。

+0

你能更具體的你需要什麼? –

+0

只需將另一個屬性添加到您提供給'data'的對象......? –

+0

@Rory McCrossan:這是一個解決方案。但是如果這樣做,我需要在後端更改$ category的條件。它會影響其他一些功能。 – django

回答

1

你可以在你的php文件中放入一個switch聲明,然後調用傳遞參數的函數。像這樣

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

在你getStoreSupplyItems功能,您可以獲取的價值作爲PARAM並且還通過$_REQUEST$_GET使用其他get PARAMS。

我希望這是你要找的。

+0

這真的很不錯。這是有用的,我可以再問一個問題如果參數正在改變會怎樣。 – django

+0

它將如何改變?你在ajax請求中發送了這個參數嗎? –

+0

是的,我需要從ajax請求發送它。對於不同的事件我需要發送不同的參數。 – django

1

要通過你的數據,你必須做出這樣的呼籲:

$.ajax({ 
    method: "POST", 
    url: "lib/function.php", 
    data: { name: "John", location: "Boston" } 
}).done(function(msg) { 
     alert("Data Saved: " + msg); 
}); 

然後,在你的PHP函數,你必須通過$ _ POST [「名」]和$ _ POST ['位置來訪問PARAMS ']