PHP people: how do I embed an HTML Link?

I’m trying to embed an HTML link to a photo into PHP. My code looks like this:

echo “<p>”;
echo “<a href= ‘htmlspecialchars(stripslashes($row[“photo_id”]))’>”;
echo “Click!”;
echo “</a>”;

When you click on the word ‘Click!’, it should open a certain JPEG photo, referenced in the table as “photo_id”. However, when I try to view the page, I get this:

First Row:

Road Number
Locomotive Type
Paint Scheme
Photographed MM-DD-YYYY
Photograph Location


Second row:

"; echo “Click!”; echo “”; echo htmlspecialchars(stripslashes($row[“road_number”])); ?>

Obviously there is a problem. Is it with escaping characters, or what?

By the way, what SHOULD the correct syntax look like?

I think you’re supposed to use PRINT instead of ECHO. It’s been a while since I’ve PHP work, but I’ve always used PRINT to write HTML to a document.

Try this.
I also like to keep my variable cleaning separate from my use. I’m pretty sure you don’t want single quotes in there, because it kills your variable.



echo "<p>";
$row_clean["photo_id"]=htmlspecialchars(stripslashes($row["photo_id"]));
echo "<a href=\"{$row_clean["photo_id"]}\">";
echo "Click!";
echo "</a>";
?>


That’s better, but I am still getting output on the page that looks like this:

"; echo “Click!”; echo “”; echo htmlspecialchars(stripslashes($row[“road_number”])); ?>


echo "<p>";
echo "<a href=\"", htmlspecialchars(stripslashes($row["photo_id"])), "\">";
echo "Click!";
echo "</a>";

End the string with ", then separate the items you want to echo with a comma. Inside the strings, use " to escape a double quote.

Or you could do it this way, by embedding HTML:


?>
<p>
<a href="<? echo htmlspecialchars($row["photo_id"]) ?>">Click!</a>
<?

Mr2001 hit the nail on the head. But I feel compelled to say – why not use a descriptive link like “photo of me and a bigmouth bass” instead of the blind and obvious “click”?

I’d try using "s instead of 's around your link in the href line. It’s easy enough to be worth a try.

I took all of your advice, and guess what? My PHP page Works! I have changed the word ‘click’ to the actual name of the photo as well. PHP is very tricky as far as escaping characters goes, but it is also very powerful. Thanks for your help, guys!

I don’t think the problem is the syntax at all (althoiugh you might run into syntax issues later - I haven’t gone over it with a fine tooth comb).

Looks to me like your web server doesn’t recognize the .php extension, and doesn’t run PHP on your file - you’re just getting your text back - minus the valid HTML tags, which effect the formatting.

You can verify this by using View->source from the browser menu (if you’re on IE). You will probably see your original source code.

If you are on a hosted server, contact your admins. If this is your own server, and it’s an APACHE server, email me (in my profile). If it’s your own IIS, get someone who knows IIS to help you :frowning:

Hope this helps

D’oh! :smack: - of course I forgot the most likely reason for your code not being interperted - you may be missing the start and end PHP tags ("<?php … ?>"), in which case the interpreter assumes everything it sees is staright text to be dumped to the browser.

Is your code in an .inc file? If so, I’m not quite certain, but I think you may need to put fresh <?php …?> tags in each include file.