mardi 29 octobre 2019

How To set `session` in ajax and use the `session` in if-statement

I want to make a session in ajax but i have not found the solution and use the session in if-statement

I have already to make a session on controller and pass to javascript script but the method is doesn't work

controller when i set the session and want pass to js function

public function set_session_js($kelas) { return $this->session->set_userdata('kelas', $kelas); }

Script JS

function load_data() {
        $('.view_data').click(function() {
            var id = $(this).attr('data-id');
            $.ajax({
                type: "GET",
                url: "<?= site_url('request/get_modal') ?>",
                dataType: "JSON",
                data: {
                    id: id
                },
                success: function(data) {
                    var output = "";

                    for (var index in data) {
                        if (data.hasOwnProperty(index)) {
                            $('#ModalShow').modal({
                                backdrop: 'static',
                                keyboard: true,
                                show: true
                            });
                            output += "<tr data-id='" + data[index]['id_form'] + "'>";
                            output += "<td data-id='" + data[index]['id_form'] + "'>" + data[index]["name_item"] + "</td>";
                            output += "<td>" + data[index]["quantity"] + " " + data[index]["satuan"] + "</td>";
                            output += "<td>" + data[index]["date_process"] + "</td>";
                            output += "<td>" + data[index]["quantity_proses"] + " " + data[index]["satuan"] + "</td>";
                            output += "<td>" + data[index]["progres_by"] + "</td>";
                            output += "<td>" + data[index]["progres"] + "</td>";
                            output += "<td>";
                            // Buat Kondisi Status
                            if (data[index]['states'] == 'On Process') {
                                output += "<label class='label label-primary'>" + data[index]["states"] + "</label>" + "</td>";
                            } else if (data[index]['states'] == 'Pending') {
                                output += "<label class='label label-warning'>" + data[index]["states"] + "</label>" + "</td>";
                            } else {
                                output += "<label class='label label-success'>" + data[index]["states"] + "</label>" + "</td>";
                            }
                            // Buat Kondisi Hak Edit dan Hapus
                            if (data[index]['states'] == 'On Process') {
                                $.get('request/set_session_js/' + kelas, function (result) {
                                    console.log(result)
                                })
                            } else if (data[index]['states'] == 'Pending') {
                                output += "<td style='text-align:center'>" +
                                    "<a href='javascript:;' class='edit' data-id='" + data[index]['id_form'] + "'><i class='fa fa-edit'></i></a>" + " | " +
                                    "<a href='javascript:;' class='hapus' data-id='" + data[index]['id_form'] + "'><i class='fa fa-trash'></i></a>" + " | " +
                                    "</td>";
                            }
                            output += "</tr>";
                        }
                    }
                    $("#show_data").html(output);
                }
            });
        });
    }

I hope that the session can be called and want to use the session in an if-statement

Aucun commentaire:

Enregistrer un commentaire