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

Zurik.Ludg's avatar

[Problem] jQuery click event with represent wrong values from Eloquent

I'm pretty much new to jQuery and I'm still testing the water. So with that I whipped out an example of when a user clicks a button tag, a jQuery UI dialog will prompt out with two button (Ok and Cancel).

Now this ui-dialog will be generated with different values from Eloquent, this value will then be stored in the 'OK' button located inside of the ui-dialog, however there's a problem..

So say there are 5 rows of result in the database. In the code below using the for loop, it will generate 5 buttons, each representing their respective rows. No matter which OK button i clicked in the UI dialog of any buttons generated, it will always be the value of the first result from the query. following by second, third and so on..

How can I generate the window.location.href value where it represent exactly the row it represents?

<tbody>
@foreach($Bookings as $Booking)
$(document).ready(function(){
$("tbody").delegate("#btn-accept", "click", (function() {
    $("<div>Hey, thanks for clicking me!</div>").dialog({
        modal: true,
        resizable: false,
        width: 450,
        title: "Greetings users!",
        buttons: {
            "Ok": function() {
                window.location.href='/clickme/{{ $Booking->id }}/accept';
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        },
        position: {
            my: "center",
            at: "center",
            of: "#section-panel"
        }
    });
}));
    <button id="btn-accept" type="button">Click Me!</button>                                            
@endforeach
</tbody>
0 likes
5 replies
shakti's avatar

Sorry but through above description your code is not clear

as per my point of view you want to show single buton on each dialog. correct me if i m wrong

Zurik.Ludg's avatar

@shakti

In the above code, It will generate 5 buttons.

If I click the first button, and click 'OK' in the UI dialog it should link me (e.g. http://localhost/1/)

Second button should link to (e.g. http://localhost/2/) and so on..

However, the problem is with the code above. If I were to click any button the first time, it will be linked to ( http://localhost/1/) and the second click to any button it will be link to (http://localhost/2/). The link should be what button i'm clicking, e.g. fourth button should link me to (http://localhost/4/)

shakti's avatar

yes the solution provide by @silverxjohn is prefect please try this and if you have any confusion please let us know

Please or to participate in this conversation.