0

in this page, I have this button (code below) that if clicked will trigger the script

button code (rolemanage.php) :

a href="<?= base_url('admin/roleaccess/') ?>" data-toggle="modal" data-target="#aksesModal" class="badge badge-warning aksesModal" data-id="<?= $r['id_lvl']; ?>" class="badge badge-warning">Akses</a>

onclick script (rolemanage.php)

$('.aksesModal').on('click', function() {

    const id = $(this).data('id');

    $.ajax({
        url: "<?= base_url('admin/getMenuid'); ?>",
        data: {
            id: id
        },
        method: 'post',
        dataType: 'JSON',
        success: function(data) {
            var strData = JSON.stringify(data);                
        }
    });
});

so the var data success get the array back from admin/getMenuid and the result arrays in console like this :

(1) […] ​ 0: Object { id: "4", menu: "Barang Habis Pakai" } ​ length: 1

then after that I tried to make that object to string using json.stringify in success: function

and the result like this :

[{"id":"3","menu":"Menu"}]

the question is : when I tried call strData in this page (rolemanage.php) using

var_dump(json_decode($_POST['strData']));

it shows undefined index strData.

how to get the strData value then ?

Loudie
  • 36
  • 8
  • Where have you defined `$id_user` and `$lv_user`? – Kinshuk Lahiri Oct 19 '16 at 06:13
  • You didn't even declare `$id_user` and `$lv_user` – Irvin Oct 19 '16 at 06:14
  • on a side note: don't ever store passwords as plain-text. it's basically the biggest no-go of handling passwords. use `password_hash()` to create a secure hash of it and `password_verify()` to check if a password is valid. – Franz Gleichmann Oct 19 '16 at 06:14
  • 1
    check [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – jitendrapurohit Oct 19 '16 at 06:15
  • 2
    What is output of `$cetak`? – AHJeebon Oct 19 '16 at 06:15
  • @KinshukLahiri i declare $id_user & $lv_user based on session value thats why i ask why getting error. – Loudie Oct 19 '16 at 06:18
  • @GustiAldi You are assigning those values to session without declaring them. – Kinshuk Lahiri Oct 19 '16 at 06:19
  • @AHJeebon its 0 or more than 1 (depend on how much user with the same value) – Loudie Oct 19 '16 at 06:19
  • `DECLARING` is different from `ASSIGNING`. You are `assigning` `$id_user` and `$lv_user` to `SESSION` without even `declaring` them. – Irvin Oct 19 '16 at 06:21
  • Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right. – VishalParkash Oct 19 '16 at 06:24
  • @VishalParkash its work, but when i tried to echo $id_user or $lv_user it didnt show anything, why ? – Loudie Oct 19 '16 at 06:31
  • 1
    try this: $id_user = '1'; $lv_user = '2'; $_SESSION['log_usr'] = $id_user; $_SESSION['sts_usr'] = $lv_user; Now try echo $id_user or $lv_user.. Still Empty ?? – VishalParkash Oct 19 '16 at 06:36
  • 1
    @KinshukLahiri and irvin why you are asking variable is not declared? Do you have any idea of function extract(); search it. [see](http://php.net/manual/en/function.extract.php) – AHJeebon Oct 19 '16 at 06:38
  • @VishalParkash i try echo 2 of them and it says 12, and thats mean my declare with value of session didnt work ? – Loudie Oct 19 '16 at 06:39
  • @AHJeebon You can see the output of that extract and beside that if it was there then the variable was declared and no error should be there. It is clearly saying that variable is not declared and hence told him to do so :) – Kinshuk Lahiri Oct 19 '16 at 06:40
  • Ok, Let us know where and what values has been assigned by you for the variables $id_user and $lv_user? You are getting confused somewhere i guess.. – VishalParkash Oct 19 '16 at 06:43
  • @VishalParkash okay, I tried to create 1 login form for 2 level authorithy (user & admin), so i tried with this code and i add session for getting value of id_user & lv_user on my table, then if i get that 2 value i can use if-else to pointed user to user page and admin to admin page. as far i know my logic didnt work on this problem :') – Loudie Oct 19 '16 at 06:47
  • Try this code after you fetch records from database ----- //Assuming that the table column are id_user and lv_user $id_user = $cetak["lv_user"]; $lv_user = $cetak["lv_user"]; $_SESSION['log_usr'] = $id_user; $_SESSION['sts_usr'] = $lv_user; Then try to echo the session or those variables $id_user and $lv_user – VishalParkash Oct 19 '16 at 06:54
  • @KinshukLahiri I tried to change my question, can you help me with my problem sir ? – Loudie Apr 03 '19 at 08:26

0 Answers0