nyotsov
nyotsov

Reputation: 93

Need to set a var into a link

I have this loop where I'm creating a link, but bootstrap modals prevents me to use PHP variables properly from the top loop (Yes a loop in loop). So I decided to use jQuery and get the id from the button when it's clicked, and that is fine, but how do I append this into the link without appending the whole link, because there is another ID variable which I'm using again in this links? Here in $value->getId() I need to append value from jQuery.

foreach ($exploredArtistBands as $band) {
    echo '<a href="/notification/invite/' . $value->getId() . '/' . $band['band_id'] . '">Join to ' . $band['name'] . '</a><br />';
}
$('.invite').click(function(){
    $(this).attr('button-id')
});

Upvotes: 0

Views: 221

Answers (1)

Dane O&#39;Connor
Dane O&#39;Connor

Reputation: 77378

You can modify the url for your link in the click event. You just have to turn your url into something that's easy to parse. Something like the following;

foreach ($exploredArtistBands as $band) {
    echo '<a href="/notification/invite/{id}/' . $band['band_id'] . '">Join to ' . $band['name'] . '</a><br />';
}

So you end up with a link like:

href="/notification/invite/{id}/band_id"

Now in your click handler, replace {id} with the id from the button.

$('.invite').click(function(){
    var id = $(this).attr('button-id');
    var url = $(this).attr("href");
    $(this).attr("href", url.replace('{id}', id));
});

The click event will continue to bubble, but with a new url. This will ultimately direct your browser to the right place. This approach smells to me, but it works.

Upvotes: 1

Related Questions