Solved

proper way to write a php variable with a hyperlink

Posted on 2011-09-28
7
346 Views
Last Modified: 2013-12-13
Can someone tell me the correct way to write this variable.

 $mess .= "<a href=".$_POST['email_site_address'].">".$_POST['email_site_name']."</a>";
0
Comment
Question by:Luey
  • 3
  • 2
  • 2
7 Comments
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36753002
I don't understand the question.  Perhaps a question from me will help -- Where do you want to write the variable?
0
 

Author Comment

by:Luey
ID: 36756502
Well actually it works but I do not understand why.
Normally you would write a hyperlink like so right?
<a href="some_site">"some text"</a>

With my variable would I not be missing the "" around the site address after href=
"<a href=".$_POST['email_site_address'].">".$_POST['email_site_name']."</a>
0
 
LVL 13

Accepted Solution

by:
Hugh McCurdy earned 125 total points
ID: 36770269
Oh, I see.  I think it works because PC based browsers are able to repair poor HTML syntax.  Don't count on mobile devices to be able to do that.

What I think you want (not being sure I understand) is to have quotes around the address and name.  This is difficult because you need to quotes to comply with PHP syntax.  Fortunately, you can escape your quotes so that they aren't used as quotes in PHP but do get used as quotes in HTML.

I'm not sure I'm typing this correctly but hopefully I'll get it right.  If it doesn't work, please echo $mess to the screen and share it here.

instead of

$mess .= "<a href=".$_POST['email_site_address'].">".$_POST['email_site_name']."</a>";

Open in new window


try

$mess .= "<a href= \"".$_POST['email_site_address']."\"> \"".$_POST['email_site_name']."\"</a>";

Open in new window



0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
ID: 36791093
You might want to learn about PHP string variables, especially the HEREDOC syntax.  I find that HEREDOC makes life very easy!
http://php.net/manual/en/language.types.string.php

I would probably go with something like this untested code snippet.

HTH, ~Ray
// INSTEAD OF $mess .= "<a href=".$_POST['email_site_address'].">".$_POST['email_site_name']."</a>";
$e = htmlentities($_POST['email_site_address']);
$n = htmlentities($_POST['email_site_name']);
$mess = <<<ENDMESS
<a href="$e">$n</a>
ENDMESS;

var_dump($mess);

Open in new window

0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36794598
Ray makes a good point about striving for better code.

The var_dump is a very good idea.  Regardless of what you do, if you are still having trouble, please share the var_dump of $mess with us.
If using Ray's code, also the var_dump of $e and $n.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36796197
And a var_dump($_POST) might be a helpful and informative step, as well!
0
 

Author Closing Comment

by:Luey
ID: 36816344
Both examples worked but I ended up using the HEREDOC syntax.  Thanks a lot.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question