Reputation: 129
I'm trying to create a textbox that will be displayed on my website. When displayed, I'd like to show some data within the text box. Here is what I have
echo "<input type=\"text\" size=\"100\" value=\"<a href=\"$url\"></a>\">";
All that shows up in the text box is <a href=
And then at the end of the text box, right after the text box I see ">
I know something must be syntactically off, just not sure what.
Upvotes: 0
Views: 138
Reputation: 137450
You have made some mistake. Your code will result in something like that (also visible in this jsfiddle):
<input type="text" size="100" value="<a href="http://www.google.com/"></a>">
Instead you can use something like that:
echo "<input type=\"text\" size=\"100\" value=\"<a href="$url"></a>\">";
or
echo '<input type="text" size="100" value="<a href="' . $url . '"></a>">';
to receive effect visible in this jsfiddle. Is it satisfying enough?
Upvotes: 0
Reputation: 10477
You must encode <
, "
, and >
chars - they can't be embedded that way. Use:
echo '<input type="text" size="100" value="'.htmlspecialchars('<a href="'.$url.'"></a>').'">';
You may also use urlencode()
function - see which suits you better.
One more tip - use single quotes when string contains HTML-like content. This will save you adding \"
everywhere.
Upvotes: 2
Reputation: 23455
Think of what the html would look like:
<input type="text" size="100" value="<a href="$url"></a>">
^
|
This is where the value attribute ends!
htmlspecialchars
should solve it.
Upvotes: 0
Reputation: 2133
php_code ?>
<input type="text" size="100" value="<a href="e;<?=$url;?>"e;></a>\">
<?php
php_code
maybe this will work for you
Upvotes: 0