Reputation: 7493
I have a json_data.php
file and I want to pass a parameter to a function inside a switch statement using getJSON
.
<?php
require_once "Db.php";
$db = new Db();
if(isset($_GET['method'])) {
switch (($_GET['method'])) {
case 'getUserIds':
echo json_encode($db -> getUserIds());
exit();
case 'getUser':
echo json_encode($db -> getUser($userId) {
exit();
// other cases go here
case 'getCertCategories':
echo json_encode($db -> getCertCategories());
exit();
case 'get
default:
break;
}
}
How do I pass a parameter so I can pass $userId
to the getUser
function?
Upvotes: 0
Views: 722
Reputation: 780879
The second argument to $.getJSON
is an object containing query parameters.
$.getJSON('json_data.php', { method: 'getUserIds' }, function(response) {
...
});
$.getJSON('json_data.php', { method: 'getUser', userId: 'joe' }, function(response) {
...
});
In PHP, you would access $_GET['method']
, $_GET['userId']
, etc.
Upvotes: 2