Use php variable in datatables in a if function to prevent the option to show colomn

Use php variable in datatables in a if function to prevent the option to show colomn

martin1223345martin1223345 Posts: 84Questions: 23Answers: 0

In php I have a variable called $status. When the user has the status 4 or higher it should be able to show the colomns email and pass (not defined at this moment). Can i use this variable in a if statement in my datatables script?

Example (i know there are syntacs mistakes in my if statement but i dont know if and how to emply in this function):

  dom: 'Bfrtip',
        buttons: [
            {
                extend: 'collection',
                text: 'Table control',
                buttons: [
                    {
                        text: 'Show Type',
                        if (  $status >= 4){ 
                        action: function ( e, dt, node, config ) {
                            dt.column( -9 ).visible( ! dt.column( -9 ).visible() );
                        } }
                    },
                    {
                        text: 'Show Plane',
                        action: function ( e, dt, node, config ) {
                            dt.column( -6 ).visible( ! dt.column( -6 ).visible() );
                        }
                    }
                ]
            }
        ]
   

      });

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    This SO thread should help - it would be best to search on there as this is a standard JS/PHP question,

    Colin

  • martin1223345martin1223345 Posts: 84Questions: 23Answers: 0

    Thanks for you reaction. I have tried it like this.

    var php_va = "<?php echo $targetlocation; ?>";
        console.log(php_va);
                    
                    $(document).ready(function() {
        
                    var table = $('#accountTable').dataTable({ ... etc etc
    

    This code shows my variable correctly in the console log so that goes well i guess.

    But now i want to use it in a input form, I can't seem to get this to work. What am i doing wrong? Tried is on more ways

    { 'data': null, title: '', wrap: true, "render": function (item) { return '<form method="post" target="_example" action="https://www.example.nl/example.php"><input type="hidden" value=php_va; name="invite" ><input type="hidden" value=' + php_va + '  name="userinvite" ><input class="example_f" type="submit" value="Invite"></form>' } },
                            
                     ], 
    
  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923
    edited February 2021 Answer ✓

    The problem is not with the variable but with the syntax of your input tag. All of the parameters need to have quotes around the values.

    In one case you have:

    <input type="hidden" value=php_va; name="invite" >
    

    If you want the value to be the string php_va then it needs to look like this: value="php_va".

    If you want the value of the variable then it needs to look like this: value="' + php_va + '".

    You can use console.log in the render function to output the string you are building to verify its correct. You can then copy it into your HTML just to see if its working.

    See this tutorial for more info on string concatenation with variables.

    Kevin

  • martin1223345martin1223345 Posts: 84Questions: 23Answers: 0

    Thank you !

This discussion has been closed.