0

I'm trying to pass dynamically keys for data option into ajax call.

data:{action:'add',id:10},

Actually I'm trying to create global function, which can be use for all tables for disable the specific rows onClick event. For that purpose I need to add different actions and table column names for calling php function. So i want to pass keys also as well as values.

JS Function

function globalDisable(id,table_column,call_action){
    // call_action is use for calling the related php function
    // table_column is use for calling the column name of that table
    action_parm = {call_action:'disable',table_column:id}
    $.ajax({
        type:'POST',
        url:'post.php',
        data:action_parm,
        success: function(response){
        }
    });
}

Call JS Function

onclick="return globalDisable(10,'user_id','disableUser');"

Php Script

$disableUser  = (isset($_REQUEST['disableUser']))? (string)$_REQUEST['disableUser']:'not';
if($disableUser != 'not'){
    $response->disArea($user_id);
}

When I run the function, in console doesn't changing its remains same call_action=disable&table_column=10.

So is this possible to pass dynamically keys for data option in ajax call? I would like to appreciate if someone guide me regarding this. Thank You

Ayaz Ali Shah
  • 3,453
  • 9
  • 36
  • 68
  • parameters should be in same order while calling function. – Nitin Dhomse Jul 20 '16 at 12:27
  • Use the debugger in your browser to **look at** the HTTP data which is being sent to the host. You should immediately recognize what the trouble is ... The data is not being sent to the host in the manner that your host-side code *(as written)* expects. – Mike Robinson Jul 20 '16 at 12:29
  • Possible duplicate of [How do I add a property to a JavaScript Object using a variable as the name?](http://stackoverflow.com/questions/695050/how-do-i-add-a-property-to-a-javascript-object-using-a-variable-as-the-name) – Andreas Jul 20 '16 at 12:29
  • show the row with function called – Pradeep Jul 20 '16 at 12:33

2 Answers2

0

Instead of action_parm = {call_action:'disable',table_column:id}, write:

var action_param = {};
action_param[call_action]='disable';
action_param[table_column]='id';

See: How do I add a property to a JavaScript object using a variable as the name?

Community
  • 1
  • 1
LeDoc
  • 935
  • 2
  • 12
  • 24
  • How it will be add next to `data:{action_param}` ? – Ayaz Ali Shah Jul 20 '16 at 12:36
  • And that will produce `disableUser = "disable"` ... not really useful – SparK Jul 20 '16 at 12:38
  • The question was : `So is this possible to pass dynamically keys for data option in ajax call?` – LeDoc Jul 20 '16 at 12:41
  • @SparK Because of this check in the php script `isset($_REQUEST['disableUser'])` the submitted parameter should be `disableUser` which is the value of `call_action` in `globalDisable(10,'user_id','disableUser')`. And that's the result of the code in this answer. – Andreas Jul 20 '16 at 14:41
  • @Andreas what's really confusing is the tableColumn part receiving a number... that would produce `1="id"`. Upvoting anyway. – SparK Jul 20 '16 at 17:28
-1

You are passing your action as a parameter to the function but then not using it in the ajax call

function globalDisable(id,table_column,call_action){
    // call_action is use for calling the related php function
    // table_column is use for calling the column name of that table

    //action_parm = {call_action:'disable',table_column:id}
    var action_parm = {call_action: call_action,
                       table_column: table_column, 
                       user_id: id};
    $.ajax({
        type:'POST',
        url:'post.php',
        data:action_parm,
        success: function(response){
        }
    });
}

You could do this like this, maybe you will find it easier to read

function globalDisable(id,table_column,call_action){
    // call_action is use for calling the related php function
    // table_column is use for calling the column name of that table

    $.ajax({
        type:'POST',
        url:'post.php',
        data:{ call_action: call_action,
               table_column: table_column, 
               user_id: id
             },
        success: function(response){
        }
    });
}

Also in the PHP you are testing for the wrong parameter in $_REQUEST

$disableUser  = (isset($_REQUEST['call_action']))? (string)$_REQUEST['call_action'] : 'not';
if($disableUser != 'not'){
    $response->disArea($user_id);
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Its giving me undefined error `ReferenceError: action_param is not defined` – Ayaz Ali Shah Jul 20 '16 at 12:38
  • It does not give me correct result in the console, I'm calling the function `return globalDisable(1, 'user_id', 'disArea');` and this is the result `call_action=disArea&table_column=1` – Ayaz Ali Shah Jul 20 '16 at 12:46
  • Thats sorrect for what you have coded. I assume you want 3 parameters passed, and you have put `id` in the wrong parameter, give me 2 mins to have a guess at what you want to do – RiggsFolly Jul 20 '16 at 12:48
  • Check the changes to the javascript, now passing **3** parameters. I am guessing a littel becasue you have not shown all your PHP code – RiggsFolly Jul 20 '16 at 12:50