回显使用php发送到数据库的微小编辑器内容

时间:2013-01-19 22:23:00

标签: php javascript jquery

我需要帮助,我需要一些帮助。我尝试在我的网页中使用tinyeditor jquery,编辑器的内容成功提交到数据库,但是使用了html标记。现在,当我尝试从数据库中检索数据并将其显示在另一个页面上时,它会将所有html标记带出内容。请问如何更正此内容以显示为格式化文本? Thankx。

使用的tinyeditor脚本

<script>
new TINY.editor.edit('editor',{
id:'input', // (required) ID of the textarea
width:584, // (optional) width of the editor
height:175, // (optional) heightof the editor
cssclass:'te', // (optional) CSS class of the editor
controlclass:'tecontrol', // (optional) CSS class of the buttons
rowclass:'teheader', // (optional) CSS class of the button rows
dividerclass:'tedivider', // (optional) CSS class of the button diviers
controls:['bold', 'italic', 'underline', 'strikethrough', '|', 'subscript', 'superscript', '|', 'orderedlist', 'unorderedlist', '|' ,'outdent' ,'indent', '|', 'leftalign', 'centeralign', 'rightalign', 'blockjustify', '|', 'unformat', '|', 'undo', 'redo', 'n', 'font', 'size', 'style', '|', 'image', 'hr', 'link', 'unlink', '|', 'print'], // (required) options you want available, a '|' represents a divider and an 'n' represents a new row
footer:true, // (optional) show the footer
fonts:['Verdana','Arial','Georgia','Trebuchet MS'],  // (optional) array of fonts to display
xhtml:true, // (optional) generate XHTML vs HTML
cssfile:'style.css', // (optional) attach an external CSS file to the editor
content:'starting content', // (optional) set the starting content else it will default to the textarea content
css:'body{background-color:#ccc}', // (optional) attach CSS to the editor
bodyid:'editor', // (optional) attach an ID to the editor body
footerclass:'tefooter', // (optional) CSS class of the footer
toggle:{text:'source',activetext:'wysiwyg',cssclass:'toggle'}, // (optional) toggle to markup view options
resize:{cssclass:'resize'} // (optional) display options for the editor resize
});

</script>

<script>
var editor = new TINY.editor.edit('editor', {
id: 'tinyeditor',
width: 500,
height: 175,
cssclass: 'tinyeditor',
controlclass: 'tinyeditor-control',
rowclass: 'tinyeditor-header',
dividerclass: 'tinyeditor-divider',
controls: ['bold', 'italic', 'underline', 'strikethrough', '|', 'subscript', 'superscript', '|',
    'orderedlist', 'unorderedlist', '|', 'outdent', 'indent', '|', 'leftalign',
    'centeralign', 'rightalign', 'blockjustify', '|', 'unformat', '|', 'undo', 'redo', 'n',
    'font', 'size', 'style', '|', 'link', 'unlink', '|', 'print'],
footer: false,
fonts: ['Verdana','Arial','Georgia','Trebuchet MS'],
xhtml: true,
cssfile: 'custom.css',
bodyid: 'editor',
footerclass: 'tinyeditor-footer',
resize: {cssclass: 'resize'}
      }
);

</script>

发布到数据库的表单

<form action="upload_poetry.php" method="post"
 enctype="multipart/form-data">
                <table width="100%" height="137" border="0" cellpadding="5" cellspacing="5">

                  <tr>
                    <td align="right" valign="top"><strong>Poem:</strong></td>
                    <td>.
                      <label for="tinyeditor"></label>
                    <textarea name="tinyeditor" id="tinyeditor" cols="45" rows="5"></textarea></td>
                </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" name="button" id="button" value="Submit"  onclick="editor.post()"/></td>
                </tr>

              </table>
              </form>

用于插入数据库的代码

<?php
include ('config.php');

$poem=$_POST['tinyeditor'];


$sql= "insert into poetry ( poem)values('$poem')";
$query=mysqli_query($dbC, $sql) or die(mysqli_error());
if ($query){

$msg = "Entry submitted pending Admin approval";
    header("location:poem.php?msg=$msg");
}
else{$msg = "Not submitted Please retry";
    header("location:poem.php?msg=$msg");}


?>

用于从数据库中检索的代码

<?php
                     include('config.php');
                     if (isset($_GET['id'])){
                     $id = $_GET['id'];
                     $sql = "SELECT * FROM poetry where title_id=$id";
                     $query = mysqli_query($dbC, $sql) or die(mysqli_error());
                     while($row = mysqli_fetch_array($query)){

                          $poem = $row['poem'];

                     }

                     ?>
                     <table width="50%" border="0" cellspacing="1" cellpadding="1">
                       <tr>
                       <td><strong>Poem:</strong></td>
                         <td colspan="4"><textarea id="" name="" ><?php echo $poem ?></textarea> </td>
                       </tr>

                     </table>
                       <?php
                     }
                     echo "<p><a href='index.php>Select an applicant to view his/her particulars</a></p>";
                     ?>

2 个答案:

答案 0 :(得分:0)

您的HTML似乎已经发送到数据库HTML编码。如果要将其作为格式化字符串取回,请使用html_entity_decode

$textFromDB = "&lt;span&gt;Some text&lt;/span&gt;";
$decodedHTML = html_entity_decode($textFromDB);
echo $decodedHTML;

这将输出一个包含文本Some text的span元素,如下所示:

<span>Some text</span>

html_entity_decode的参考:http://php.net/manual/en/function.html-entity-decode.php

该页面上还有一个指向htmlentities函数的链接,用于处理字符串的HTML编码。

答案 1 :(得分:0)

谢谢大家。我能够通过使用html_entity_decode代码解决问题,如下所示$ poetry是从数据库中检索的数据

                         `$orig = $poetry;
                          $a = htmlentities($orig);

                        $poem = html_entity_decode($a);`

并在div标签而不是textarea中回显结果。

<div> <?php echo $poem; ?> </div> 

非常感谢您的帮助