• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 945
  • Last Modified:

preserving the <b> </b&gt tags

Hi,

Im using the following code to edit an xml file on my server:

<html>
<head>
<script type="text/javascript">

function insertString(el,str){
// Mozilla XUL extensions to the DOM    
if(typeof el.selectionStart=='number')
el.value=el.value.slice(0,el.selectionStart)+str+el.value.slice(el.selectionEnd,el.value.length);
// IE    
else if(document.selection){
el.focus();
document.selection.createRange().text=str;
}
// no support    
else el.value+=str
}

</script>
</head>
<body>
<?php
$XMLfile = "content.xml";
if(isset($_POST['submit'])){ // Saving data

    $fh = fopen($XMLfile,"w");
    fwrite($fh,stripslashes($_POST['fileContent']));
    fclose($fh);
    echo "Data saved in $XMLfile";
}
else{  // Show form
    $fh = fopen($XMLfile,"r");
    $data = fread($fh,filesize($XMLfile));
    fclose($fh);
   ?>
    <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
    <textarea id="fileContent" name="fileContent" cols="50" rows="30"><? echo $data; ?></textarea>
     <input type="button" value="Bolden Text" onclick="insertString(document.getElementById('fileContent'),'&lt;b&gt; &lt;/b&gt;')">
    <input type="submit" name="submit" value="Save" onClick="return confirm('Click OK to save the data')">
  </form>
    <?
}
?>
</body>
</html>

in this section of code:

 <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
    <textarea id="fileContent" name="fileContent" cols="50" rows="30"><? echo $data; ?></textarea>
     <input type="button" value="Bolden Text" onclick="insertString(document.getElementById('fileContent'),'&lt;b&gt; &lt;/b&gt;')">
    <input type="submit" name="submit" value="Save" onClick="return confirm('Click OK to save the data')">
  </form>

I im using javascript to insert the &lt;b&gt; &lt;/b&gt; tags to the text area. my problem is that when i clcik the button these tags appear as the <b> </b> which doesnt work for me. i need them in the escape character format.

any ideas how i might resolve this?

thanks
0
clonmelog
Asked:
clonmelog
  • 3
1 Solution
 
clonmelogAuthor Commented:
ive been told that a function called htmlentities could help me here.... im ot too sure though!
0
 
RoonaanCommented:
You can then use

&amp;gt; and/or &amp;lt;

-r-
0
 
xberryCommented:
You have to escape the & sign itself first.
So put this sequence as your string instead of '&lt;b&gt; &lt;/b&gt;' :

'&amp;lt;b&amp;gt; &amp;lt;/b&amp;gt;'
0
 
clonmelogAuthor Commented:
but doesnt this just create <> tags? how does this prevent the <b></b> appearing?

0
 
clonmelogAuthor Commented:
ill try that now:

You have to escape the & sign itself first.
So put this sequence as your string instead of '&lt;b&gt; &lt;/b&gt;' :

'&amp;lt;b&amp;gt; &amp;lt;/b&amp;gt;'
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now