PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 26, 2022

[FIXED] how can i set url of route from AJAX

 February 26, 2022     ajax, laravel, laravel-routing     No comments   

Issue

WEB (route) :

Route::get('/ajax-cat/edit/{id}', [App\Http\Controllers\AjaxCRUDController::class, 'categoryEdit'])->name('ajax.categoryEdit');

AJAX Code:

    $(document).ready(function () {
    $("#categoryBtn").click(function () {
        $("#catTable").show();
        let html = '';
        let i = 0;
        $.ajax({
            url: '/ajax-cat',
            type: "GET",
            success: function (data) {
                for (const x of data) {
                    html += `<tr>
                                <th scope="row">${++i}</th>
                                <td>${x.name}</td>
                                <td><a href="{{route('ajax.categoryEdit',${x.id})}}" class="btn btn-danger">Edit</a></td>
                            </tr>`;
                }
                $("#catTableBody").html(html);
            }
        });

    });
});

Picture from Browser:

enter image description here

But I want Link like this: 127.0.0.1:8000/ajax-cat/edit/2


Solution

You cannot merge different language syntax like this.

{{ }} is from Blade Directive. https://laravel.com/docs/8.x/blade

x.id

is a variable defined in your javascript code block.

What you can do is:

var url = '{{route("ajax.categoryEdit", ":id")}}';
url = url.replace(':id', x.id);

Then you can concat your javascript variable inside your html code.



Answered By - calebe santana
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing