Navaneeth Sen
Navaneeth Sen

Reputation: 6466

How to append href strings with variables in html with PHP?

I have a list of href items as shown below:

<header class="less-header" role="banner">
    <h1><a href="#">Grid Options</h1>
    <ul>
        <li><a href="test.php?user=SORTENDDATE">SORT by END DATE</span></a></li>
        <li><a href="test.php?user=SORTSTARTDATE">SORT by START DATE</span></a></li>
        <li><a href="test.php?user=SORTZA">SORT Z to A</span></a></li>
        <li><a href="test.php?user=SORTAZ">SORT A to Z</span></a></li>
    </ul>
</header>

Now I want to add a variable also to the href string so that it becomes:

<?php
$Custom = "APPS";
?>  

<header class="less-header" role="banner">
    <h1><a href="#">Grid Options</h1>
    <ul>
        <li><a href="test.php?user=SORTENDDATE&($Custom)">SORT by END DATE</span></a></li>
        <li><a href="test.php?user=SORTSTARTDATE">SORT by START DATE</span></a></li>
        <li><a href="test.php?user=SORTZA">SORT Z to A</span></a></li>
        <li><a href="test.php?user=SORTAZ&($Custom)">SORT A to Z</span></a></li>
    </ul>
</header>

How to append href strings with variables in html using php?

I tried this much:

Upvotes: 0

Views: 5300

Answers (3)

indubitablee
indubitablee

Reputation: 8206

and via php

<?php
$Custom = "APPS";
?> 


<h1><a href="#">Grid Options</h1>
<ul>
    <li><a class="custom" href="test.php?user=SORTENDDATE&<?php echo($Custom);?>"><span>SORT by END DATE</span></a></li>
    <li><a href="test.php?user=SORTSTARTDATE"><span>SORT by START DATE</span></a></li>
    <li><a href="test.php?user=SORTZA"><span>SORT Z to A</span></a></li>
    <li><a class="custom" href="test.php?user=SORTAZ&<?php echo($Custom);?>"><span>SORT A to Z</span></a></li>
</ul>

Upvotes: 2

Rhumborl
Rhumborl

Reputation: 16609

Unless I'm missing the point, you almost have it, you just need to put the $Custom variable in php echo tags...

<?php
$Custom = "APPS";
?>

<header class="less-header" role="banner">
    <h1><a href="#">Grid Options</h1>
    <ul>
        <li><a href="test.php?user=SORTENDDATE&<?= $Custom ?>">SORT by END DATE</span></a></li>
        <li><a href="test.php?user=SORTSTARTDATE">SORT by START DATE</span></a></li>
        <li><a href="test.php?user=SORTZA">SORT Z to A</span></a></li>
        <li><a href="test.php?user=SORTAZ&<?= $Custom ?>">SORT A to Z</span></a></li>
    </ul>
</header>

As Grace Lee noted, you have closing </span> tags without opening ones which you should look at correcting, but that isn't relevant to the question.

Upvotes: 1

indubitablee
indubitablee

Reputation: 8206

if you decide to do it via jquery:

    <h1><a href="#">Grid Options</h1>
    <ul>
        <li><a class="custom" href="test.php?user=SORTENDDATE&"><span>SORT by END DATE</span></a></li>
        <li><a href="test.php?user=SORTSTARTDATE"><span>SORT by START DATE</span></a></li>
        <li><a href="test.php?user=SORTZA"><span>SORT Z to A</span></a></li>
        <li><a class="custom" href="test.php?user=SORTAZ&"><span>SORT A to Z</span></a></li>
    </ul>

    $('.custom').each(function(){
        var url = $(this).attr('href');
        var newUrl = url + 'APPS';
        $(this).attr('href', newUrl);
    });

And the fiddle with it: http://jsfiddle.net/swm53ran/9/

Upvotes: 1

Related Questions