Reputation: 57
Greetings for the day.
I am in the middle of a coupon based website development. As part of it, once the user clicks on the get coupon button, I need to display to the user the coupon code as well as redirect them to the merchant website. To achieve this I am fetching all the details from the database and storing them in php variables, then displaying it in the web page.
echo '
<div class="col-sm-3 feature" >
<div class="panel" id="bar">
<div id="propost">
<a href="', $LINK, '">
<img src="', $IMG, '" class="img-responsive" alt=""/></a>
<div class="special-info grid_1 simpleCart_shelfItem">
<h5>', $POSTDESC, '</h5>
<div id="btnstyle"> </div>
<a href="', $LINK, '" target="_blank" class="', $btn, '" onclick="window.open(', $ccodelink, ');">', $text, '</a>
</div>
</div>
</div><!-- end panel -->
</div><!-- end feature -->';
Everything is working as expected apart from the multiple links which I mentioned in the anchor tag. How do I do this?
Upvotes: 0
Views: 537
Reputation: 6946
Here is an example of different approach using non-obstrusive javascript :
HTML
<a href="link1" data-secondlink="link2">This is my link</a>
JAVASCRIPT (jQuery)
$('a[data-secondlink]').on('click',function(){
window.open($(this).data("secondlink'));
});
EXPLANATION
Unobstrusive javascript is just a best-practice and the many reasons why you should prefer this can easily be found online. The main I would point in this case is that in case requirements change (let's say you need to use a lightbox instead of a new tab/window for example), you just have to update 1 JS file instead of all your PHP/HTML files.
As of the line of code, on click it executes this line, and afterwards it executes the default action of an anchor : requesting the URL in the href
attribute.
Upvotes: 0
Reputation: 5919
You might try this:
<a href="url1" onclick="window.open('url2');">Click</a>
Updated:
echo '<a href="' . $url1 . '" onclick="window.open('' . $url2 . '');">Click</a>';
Also: don't put block elements like div
s into anchors. Thats not valid html :)
Upvotes: 1
Reputation: 4160
You are doing concatenating string via ,
replace , with . in Anchor tag and window.location()
Upvotes: 0