Concatenate a javascript variable in a path

0

I am working in laravel, creating a verification to be able to delete a record The JS code

function confirmModal( $func,$id)
{
                var message = "guardar";
                if ($func == "eliminar")
                    message = "eliminar";
                bootbox.confirm({
                    message: "¿Desea " + message + " el registro?",
                    buttons: {
                        confirm: {
                            label: 'Si',
                            className: 'btn-success'
                        },
                        cancel: {
                            label: 'No',
                            className: 'btn-danger'
                        }
                    },
                    callback: function (result) {
                        if ( result )
                        {
                             document.location.href="{!! route('admin.users.destroy', '$id' )!!}"; //esta es la ruta para laravel, pero no puedo concatenar el id que le paso a través de onclick 

                        }
                        $('.modal-open .modal').css({"overflow-x": "hidden", "overflow-y": "auto"});
                    }
                });

}

.. some solution for my problem .. Thank you in advance

    
asked by Luis Aguilar 29.08.2018 в 20:53
source

2 answers

0

you are passing a php variable to a function written in javascript

try this way, but organize it at your convenience because it is set id and function outside the function

var id = "<?php echo $id; ?>";
var func = "<?php echo $func; ?>";

function confirmModal( func,id)
{



    var message = "guardar";
                if (func == "eliminar")
                    message = "eliminar";
                bootbox.confirm({
                    message: "¿Desea " + message + " el registro?",
                    buttons: {
                        confirm: {
                            label: 'Si',
                            className: 'btn-success'
                        },
                        cancel: {
                            label: 'No',
                            className: 'btn-danger'
                        }
                    },
                    callback: function (result) {
                        if ( result )
                        {
                             document.location.href="{!! route('admin.users.destroy', 'id' )!!}"; //esta es la ruta para laravel, pero no puedo concatenar el id que le paso a través de onclick 

                        }
                        $('.modal-open .modal').css({"overflow-x": "hidden", "overflow-y": "auto"});
                    }
                });

}
    
answered by 29.08.2018 в 21:08
0

Assuming you have a route like this:

Route::get( '/user/destroy/{id}', 'TuControlador@tuFuncion')->name('admin.users.destroy');

Your javascript code would look like this:

function confirmModal( $func,$id)
{
                var message = "guardar";
                if ($func == "eliminar")
                    message = "eliminar";
                bootbox.confirm({
                    message: "¿Desea " + message + " el registro?",
                    buttons: {
                        confirm: {
                            label: 'Si',
                            className: 'btn-success'
                        },
                        cancel: {
                            label: 'No',
                            className: 'btn-danger'
                        }
                    },
                    callback: function (result) {
                        if ( result )
                        {
                             document.location.href="{{ route('admin.users.destroy', ['id' => $id] ) }}"; //esta es la ruta para laravel, pero no puedo concatenar el id que le paso a través de onclick 

                        }
                        $('.modal-open .modal').css({"overflow-x": "hidden", "overflow-y": "auto"});
                    }
                });

}

NOTE: In order to execute php code within a script, the javascript code must be inside the file .blade.php

    
answered by 29.08.2018 в 22:22