0

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:

4
  • 2
    Where is your variable defined - are you using a server side language such as PHP, or just Javascript? You cannot do this with pure HTML Commented Dec 31, 2014 at 13:52
  • It is defined inside php. So I can only do this with PHP Commented Dec 31, 2014 at 13:56
  • u mean u want a code to read the href then add the ($Custom) automatically to it or do u mean that u tried the 2nd example and it didn't work ? Commented Dec 31, 2014 at 14:00
  • your span tags arent opened btw...you just have floating </span> closing tags. and you can do this in javascript regardless of where the variable is defined, fyi, but it is cleaner to accomplish this where the variable is defined Commented Dec 31, 2014 at 14:00

3 Answers 3

2

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.