Morning everyone --

Can anyone help me with what is probably very simple?

I have a page where I want to get a variable from the URL and insert it in the buy button... so if my url is http://mywebsite.com/hangout?id=beauty

I want when someone click the button Buy to go to

Thanks in advance!

Dani AI

Generated

Quick recap and safer options (builds on what and suggested)

was right that the link needs to be generated (or updated) with server-side or client-side code; 's session idea is fine as a source of the id, but the example that mixes header() inside an onclick is incorrect — header() is a server-side redirect and must run before any output. Below are two practical, safer approaches: a server-side PHP solution that validates/encodes the id, and a client-side JavaScript alternative for cases where you prefer not to rebuild the page on the server.

Server-side (PHP) — validate and encode before you output the link:

<?php
// hangout.php (server-side)
$id = isset($_GET['id']) ? trim($_GET['id']) : '';
$allowed = ['beauty','fashion','tech']; // replace with your allowed values
if (!in_array($id, $allowed, true)) {
    $id = preg_replace('/[^A-Za-z0-9_-]/', '', $id);
    if ($id === '') $id = 'default';
}
$target = 'https://affiliate.example/join?id=' . rawurlencode($id);
?>
<a href="<?php echo htmlspecialchars($target, ENT_QUOTES, 'UTF-8'); ?>">Buy</a>

Client-side (JavaScript) — useful when the page is static or you want to set the button after load:

<a id="buy-link" href="#">Buy</a>

<script>
const params = new URLSearchParams(location.search);
const id = params.get('id') || 'default';
document.getElementById('buy-link').href = 'https://affiliate.example/join?id=' + encodeURIComponent(id);
</script>

Notes and troubleshooting

  • Always validate or whitelist id values to avoid abuse or unintended redirects.
  • Encode the query component with rawurlencode/encodeURIComponent and escape the output with htmlspecialchars to prevent XSS.
  • If you use sessions, call session_start() at the very top of the script and still validate the stored value.
  • Prefer an <a> for navigation; use a <button> only if the click performs a state-changing action.

Recommended Answers

All 5 Replies

In my view alone html is not capable of doing so.
What is your server side language?

As default it's php. Thanks

In your file you can read passed get or post variables using php arrays $_GET AND $_POST in your case, its get variable id

Now we set new link in your hangout.php as following

hangout.php

<a href='<?php echo $_GET['id']?>' > click here to join </a>

Hi,
If the id is stored in session you can write the link like this:

<?php
    session_start();
?>
.... part of the page .....
<a href='<?php echo $_SESSION['id']?>' > click here to join </a>
... rest of the page ....

or a button

<button id="join_button" onclick="<?php header( 'Location:  '.$_SESSION['id'])">Join</button>

Thank you Utrivedi, It works like a charm

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.