How do i insert a mailto link around a php variable?

I have a routine that shows records from a database. It works perfectly well, but one of the fields is a (plain text) email address saved in a PHP $row variable. How do I make that variable a ‘mailto’ link so that it becomes clickable? Here’s the code:

<?php echo $row['emailadd']; ?>

Many thanks in advance!

Here’s the basic syntax of a mailto link -

<a href="mailto:{email address}">{clickable text or image}</a>

You would output the $row[‘emailadd’] where the {email address} is, and output something meaningful where the {clickable text or image} is.

Cheers, that was quick!
That’s the very thing I cant solve - the {clickable text or image} IS $row[‘emailadd’].

Does that mean I would need to use $row[‘emailadd’] twice? Once as the {email address} and again as the {clickable text} for it? I know I have done this before when I worked in industry but that was a while ago, and I cant recall how I did it!

You can use $row[‘emailadd’] as many times are you want.

Cheers, I shall give that a try later today (just gone midnight here in the UK!)

Nope, sorry (genuinely!) Having tried all sorts of combinations, nothing seems to work and throws syntax errors. The fact that the link text is itself a PHP variable which contains single quotes is the confusing factor.
It’s difficult to know where to close and restart various echo statements, issues with types of string quotes, and the placement of PHP tag pairs around the HTML tags.
So I think I’ll just leave things as they are before my brain melts down! But thanks for your suggestions.

There are 4 ways of producing this output.

  1. Drop out of php mode at the start of the html document and only output the dynamic parts of the html document using php code. You can use php’s short open echo tag <?= and leave out the closing ; right before a closing php tag ?>, so that echoing a variable looks like <?=$var?>
  2. Echo each individual piece of the output. This would take 5 echo statements.
  3. Put the variable(s) inside of an overall double-quoted string. Associative array elements require putting {} around the variable in the string so that php knows where the variable starts and ends. This will likely clear up the syntax errors you are getting.
  4. Use string concatenation to build the output. This results in the most error prone syntax due to all the extra quotes and concatenation dots.

Method #1 is the preferred use.

Also, you need to apply htmlentities() to any dynamic value you are outputting in a html context to help prevent cross site scripting.

<?php

// main php code for the page


// start of html document
?>


<!-- at the point of producing the link -->
<a href="mailto:<?=htmlentities($row['emailadd'])?>"><?=htmlentities($row['emailadd'])?></a>

Holy mackerel! That worked!
Thank you so much, not least for staying with me on this. It must have felt a bit frustrating for you at times. Respect, with a cherry on top!

Sponsor our Newsletter | Privacy Policy | Terms of Service