OK.. so this is probably basic but, I can't seem to wrap my head around it.

The URL is like this.. http://Whatever.com?id=ThisID&title=AncorText

So.. I'm trying to use..

<?php
 
$id = $_GET['id'];
$title = $_GET['title'];

echo $id;
echo $title;

?>

.. to place the variable parameters into a hyperlinked URL on the page where the above URL terminates..

I want the hyperlink on the page to look like this..

<a href="http://SomeOtherURL.com/ThisID/">AncorText</a>

The problem is that I can't seem to figure out how to call the variables into the hyperlink code without popping syntax errors..

Anyone feel like making me look stupid?..

Dani AI

Generated

— the syntax errors come from mixing quotes and trying to cram an entire HTML tag into a single double‑quoted PHP string. was on the right track with escaping, but escaping inside large strings gets brittle fast. A clearer, safer pattern is: read the GET values, build the URL as a normal PHP string, URL‑encode the path parts, and then HTML‑escape the final output when you emit the anchor. That avoids backslashes and prevents XSS.

<?php
$id    = isset($_GET['id'])    ? $_GET['id']    : '';
$title = isset($_GET['title']) ? $_GET['title'] : 'Link';

$url = 'https://example.com/' . rawurlencode($id) . '/';

echo '<a href="' . htmlspecialchars($url, ENT_QUOTES, 'UTF-8') . '">' .
     htmlspecialchars($title, ENT_QUOTES, 'UTF-8') .
     '</a>';
?>

Extra tips: prefer rawurlencode for path segments and urlencode or http_build_query for query strings; use htmlspecialchars(..., ENT_QUOTES, 'UTF-8') on anything echoed into HTML; use single‑quoted outer strings or concatenation to avoid escape clutter; or break out of PHP and write the <a> tag in plain HTML with short <?= ?> echoes. If things still look wrong, view the page source or var_dump() the variables to see exactly what the output is before the browser renders it.

Recommended Answers

All 2 Replies

Member Avatar for Member #120589
echo "<a href=\"">$title</a>";

lol.. thanks Mister.. makes me look stupid in all of 15 minutes :-D

I was forgetting to use the forward slashes.

echo "<a href=\"">$title</a>";
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.