Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

petritr's avatar
Level 15

How can i pass the id in ajax url

I need to pass the id in ajax, i have something like:

url: '{{ URL::route(delete_hub{{id}}) }}',

or

url: '(delete_hub{{id}}',

I got an strange url when i call the ajax, how can i pass the id correctly ?

When ajax is called i got the url like: delete_hub%7B%7B%20id%20%7D%7D

0 likes
8 replies
MaverickChan's avatar
url: 'delete_hub' + {{ id }},

btw , which javascript are you using? jquery ? pure js? or vue?

petritr's avatar
Level 15

jquery, that will output syntax error.

petritr's avatar
Level 15

I need to grab the id without having to save to js variable then pass if as data, this way i can. I want to do it a bit smarter, just grab it.

mahmoud-abdelfadeil's avatar

#In the HTML form

You write the route name with the id as an empty value
action= "{{route('name route','')}}"

#in JQuery

var url= $('form').attr('action')+'/'+id;

    $.ajax({
        url:url,
        method:'post',
        data:data,
        success:function (res) {
            console.log(res)
        }
    })
Shaan24k's avatar
$.ajax({
        url:"{{route('user.delete', '')}}/"+id,
        method:'post',
        data:data,
        success:function (res) {
            console.log(res)
        }
    });
Shaan24k's avatar

@Snapey either you can pass the global variable in javascript to access in .js file. it depends on situation.

Please or to participate in this conversation.