Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with Inserting Anchor Tags as Simple Text in a TextArea 1

Status
Not open for further replies.

entropiii

Programmer
Mar 11, 2010
3
US
I am creating a simple, small CMS module for a client. I created a little form and when they click Submit, it goes straight out into an include (.inc) file, which is connected to the web page to be displayed.

One of the buttons I want to insert an example URL so they don't have to mess around with tags. I need it to exactly insert this into the textarea:

<a href=" Link Text Here </a>

Since the data from the textarea gets passed into an html page, I need to be able to pass the anchor tags too. The trouble is, the string (highlighted below) wants to read it as an actual URL and not just spit it out as plain old text. I can make it populate plain old text - that works just fine. But instead it acts like I am trying to put actual html in there... Is there some special trick I am missing?


<form method="post" action="<?=$_SERVER['PHP_SELF']?>">

<script type= "text/javascript">

// myField accepts an object reference, myValue accepts the text string to insert at the cursor

function insertatcursor(myField, myValue) {
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
else if (myField.selectionStart == 0 || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0,startPos) + myValue + myField.value.substring(endPos, myField.value.length);
}
else {
myField.value += myValue;
}
}
</script>


<table width="50%" border="0" align="center" cellpadding="1" cellspacing="1" valign="top">
<tr>
<td width="2%"></td>
<td align="center" valign="top">
<textarea id="mytext" name="savecontent" cols="50" rows="10" style="text-align: left; padding: 0px; overflow: auto; border: 3px groove; font-size: 12px"><?=$loadcontent?></textarea></td>
<td align="left" valign="top" width="60%">
<input name="url" type="button" value="insert url" onclick="insertatcursor(document.getElementById('mytext'), '<a href=" Text</a>'); return false;">
</td>
</tr>
<tr>
<td></td>
<td align="center" valign="top"><input type="submit" name="save_file" value="SAVE"> </td>
<td align="left" valign="top"> </td>
</tr>
</table>

<br />
</form>
 
>onclick="insertatcursor(document.getElementById('mytext'), '<a href="[ignore][/ignore]">Link Text</a>'); return false;"
Like this.
[tt]onclick="insertatcursor(document.getElementById('mytext'), '\x3ca href=\x22[ignore][/ignore]x22>Link Text\x3c/a\x3e'); return false;"[/tt]
 
Awesome!! Im not THAT much of a n00b, am I?

Not asking for a lesson here, but what is that??? I havent seen it before. It worked like a CHARM, thank you!
 
It is the use of the escape sequence \xnn for ascii (where nn is the hex of the character within the charset currently in use) to avoid the collision of delimiters quot and apos. Hence, \x22 for the literal (double-)quote. The angle brackets are not straightly needed to be replaced by their escape sequence. (In fact, I forgot to escape them all leaving one ">" up there due to incomplete editing when posting.) You can simply use < and > at the place of \x3c and \x3e everywhere, or escape them all. Both scheme are fine. (My leaving one ">" there can create confusion, too bad!)
 
Ahh! I could tell it was some sort of escape code, but I couldnt tell what kind.

And yes, leaving the brackets did not seem to affect the success of it, the quotes were really the trouble.

Initially, I tried this:

&lt;a href=" &gt; Link Text &lt;/a&gt;

But that made it too literal and passed the value completely as-is to the html page, so it didnt come out as a link. Duh.

You solution worked beautifully - and it gave me another angle to look at when troubleshooting as well. Thanks for your help!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top