Let's say a user goes to example.com/example.html?one=foo
How could I make a link so that it appends another url variable like: example.com/example.html?one=foo&two=bar
I've tried <a href="?two=bar">Click here</a> but that just replaces the existing variable with a new one

How could I do this?
Thanks in advance

Recommended Answers

All 6 Replies

You'll need some scsripting to do this. I don't think it's possible with plain HTML.

Didn't think it would be. Any ideas how?

Prefrably javascript but PHP would be fine

You want a link that links back to the same page, but with an extra variable tacked on? Did I understand correctly?

<?php 
    $thisPage = htmlentities($_SERVER['PHP_SELF']);
    $hasVar = strpos($thispage, '?');
    if($hasVar === FALSE)   // Check if there is already a variable on the URL
        $url = $thisPage . '?two=bar';  // if not, use ? to start query string
    else
        $url = $thisPage . '&two=bar';  // if yes, use & to append variable
?>
<a href="<?php echo $url; ?>">link</a>

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.