Jump to content
MrsVodes

Hyperlink on email address from database element

Recommended Posts

So this is the code prior to trying to add a hyperlink

echo "<p>Name: " . $item["first_name"] . " " . $item["last_name"] . "<br> Email: " . $item ["email"]  . "</p>";

I thought that putting the <a href around the $item['email'] would make the email address a hyperlink but it does not ... it skips the email addresses completely and hyperlinks the names because all fields are $item.

echo "<p>Name: " . $item["first_name"] . " " . $item["last_name"] . "<br> Email: " . "<a href=\"mailto: {$item ["email"]} \">"  . "</p>";   

Thank you for any assistance.

Share this post


Link to post
Share on other sites
Posted (edited)

First off, I would suggest eliminating all the escaping gymnastics. It is harder to read, more prone to error and not as clean as not doing it. As to your problem, you are missing the basic syntax of a hyperlink, the closing </a>.

Here is a cleaned up version. Take note of the single quotes on the elements.

echo "<p>Name:{$item['first_name']} {$item['last_name']}<br> Email: <a href='mailto: {$item['email']}'>{$item['email']}</a></p>";  

 

Edited by benanamen

Share this post


Link to post
Share on other sites

While Benanamen's re-write is a better approach I thought I could help you understand the original problem with this tid bit.  The reason your email address 'disappeared' is because when you built the anchor tag around it, you left out the anchor tag piece that shows you "the link" literal.  That's the part that Benanamen added during his re-write of your code, between the <a......> and the </a>.

Another good way (IMHO) of writing this code is to use heredocs construct which is great for outputting a lot of html and php code that is mixed in.

$code= <<<heredocs
<p>
Name: $item['first_name'] $item['last_name']
<br> 
Email: <a href="mailto:$item['email']">$item['email']</a>
</p>
heredocs
echo $code;

  

For more in the PHP manual on using 'heredocs' go HERE

Share this post


Link to post
Share on other sites

Thank you both for your assistance. I am unable to get either of the responses to work.  It hits a page not working error on the first and has a formatting error in the second so I did not try to upload to URL.

Share this post


Link to post
Share on other sites

Show us what you changed to match us. 

Do you have error checking turned on and have an error message that points to a certain line?

Share this post


Link to post
Share on other sites

oops - I see that I left the semi off my ending heredocs line.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.