saved html in mysql db displays as plain text when retrieved

Last post 09-08-2010, 1:10 PM by Eric. 7 replies.
Sort Posts: Previous Next
  •  09-06-2010, 12:50 PM 63855

    saved html in mysql db displays as plain text when retrieved

    I've just installed CuteEditor on a PHP site that stores notes in a MySQL database table.
    I've created a test note using CuteEditor. The html is storing precidsely in the table field, but when I retrieve it for display the web page literally displays the html code.
     
    Here's the code generated by CuteEditor  stored in my MySQL db record in a field called "note":
     
    <div><h3>This is my first note.</h3></div><div>&nbsp;</div><div><span style=\"color: red;\">My list:</span></div><div>&nbsp;</div><ol><li>one</li><li>two</li><li>three</li></ol><div>&nbsp;</div><div><img alt=\"\" src=\"/mysite/uploads/WashingtonCaYubaRiver.jpg\" width=\"450\" height=\"600\" />&nbsp;</div>
    <div>&nbsp;</div>
    <div><br />
    &nbsp;</div>
     
    The calling webpage displays that html code, not the expected output. I've cut and pasted the code into my html wyswyg editor and it displays perfectly.
     
    Here the context of the code in the webpage:
     
    $note = forDisplay( fromStorage( $row[2] ) );
    echo "<tr><td valign=\"top\" align=\"right\"><p><strong>Note:</strong></p></td><td align=\"left\">".$note."</td></tr>";
     
    ($row[2] is the stored field location in a query of the table holding the note. It's a "text" field in the table.
    I simple assign it to $note and try to display it within the surrounding html on the webpage, just as I've always done with other retrieved field data, but it just displays the actual html code.
     
    Here's the unexpected output:

    Note:

    <div><h3>This is my first note.</h3></div><div>&nbsp;</div><div><span style="color: red;">My list:</span></div><div>&nbsp;</div><ol><li>one</li><li>two</li><li>three</li></ol><div>&nbsp;</div><div><img alt="" src="http://cutesoft.net/mysite/uploads/WashingtonCaYubaRiver.jpg" width="450" height="600" />&nbsp;</div>
    <div>&nbsp;</div>
    <div><br />
    &nbsp;</div>
     
    I know this has to be something quite simple. Ideas?
     
     
  •  09-06-2010, 1:42 PM 63857 in reply to 63855

    Re: saved html in mysql db displays as plain text when retrieved

    I found the problem:
     
    The display function I was using contains two sub functions that are normally needed to display text in my web pages.
     
    function forDisplay( $text ) {
      $text = htmlentities( $text );
      $text = nl2br( $text );
      return $text;
    }
    I removed this display function that formats the field data.
     
    Before:
     
    $note = forDisplay( fromStorage( $row[2] ) );
     
    After (I removed the "forDisplay" wrapping function):
     
    $note = fromStorage( $row[2] )
     
    I used to php code years ago, so my code may look antiquated (in fact I've never used OOP PHP code!)
     
    The webpage now looks correct using the CuteEditor stored html.
     
     
     
  •  09-06-2010, 3:29 PM 63858 in reply to 63857

    Re: saved html in mysql db displays as plain text when retrieved

    Dear mp5802,
     
    The following code shows how to read write content between cuteeditor and mysql: 

    <?php include_once("cuteeditor_files/include_CuteEditor.php") ; ?>
    <html>
    <head>
    </head>
    <body>
    <form name="theForm" action="1.php?postback=true" method="post"><?php
    function getData()
    {
     $cnx = mysql_connect('localhost','root','');
     mysql_select_db('cuteeditortest');
     $records = @mysql_query('SELECT ID, content FROM acid');
     if (!$records) {
      return "data is empty";
     }
     if ($record = mysql_fetch_array($records)) {
      $id   = $record['ID'];
      $content = $record['content'];
      return $content;
     }
    }
    function writeData($content)
    {
     $connect = @mysql_connect( "localhost", "root", "" );
     if ( ! $connect ) {
      die( "Couldn't connect to MySQL: ".mysql_error() );
     }
     $db = "cuteeditortest";
     @mysql_select_db( $db ) or die ( "Couldn't open $db: ".mysql_error() );
     $sql = "INSERT INTO acid( id, content ) values ('55','$content')";
     mysql_query( $sql, $connect ) or die ( "INSERT error: ".mysql_error() );
     mysql_close( $connect );
    }
    ?> <?php
    $editor=new CuteEditor();
    $editor->ID="Editor1";
    $editor->EditCompleteDocument=true;
    if (@$_GET["postback"]!="true")
    {
     $editor->Text = getData();
     $editor->Draw();
    }
    else
    {
     writeData($_POST["Editor1"]);
     $editor->Draw();
    }
    $editor=null;
    //use $_POST["Editor1"]to retrieve the data
    ?> <textarea name="textbox1" rows="2" cols="20" id="textbox1"
     style="font-family: Arial; height: 250px; width: 730px;">
    <?php echo @stripslashes($_POST["Editor1"]) ;?>
                </textarea></form>
    </body>
    </html>

    Thanks for asking
  •  09-08-2010, 12:06 AM 63876 in reply to 63858

    Re: saved html in mysql db displays as plain text when retrieved

    As I said, I've never written any php object code - just plain old procedural PHP, so I'm afraid that your reply does not help me. Perhaps, incorporating CuteEditor is beyong my php skills.
     
    I read all of your code and only understood small parts of it. Again, my problem is not storing and retrieving the actual content (maybe); it's just  that the content created by CuteEditor is displayed as raw html. 
     
    I've got 700 important notes stored in this database over ten years, including data captured in textarea forms and I don't have a problem with displyaing those records - just the ones captured and stored with the new CuteEditor.
     
    Cheers.
     
    - Mike
  •  09-08-2010, 12:30 AM 63877 in reply to 63876

    Re: saved html in mysql db displays as plain text when retrieved

    I do see now that in looking at the actual data in mysql that almost all my previous notes were stored as plain text. (They were exported
    from another old windows-based note program into my current php db - I'm trying to put these notes online.)
     
    The few old notes that are stored as html also display that html (same problem as data retrieved from a note created in CuteEditor).
     
    So, I'm guessing that any solution for me will have to incorporated some logic to differientiate between the legacy notes stored
     as plain text and those with embedded html.
     
    To be perfectly clear, this webpage is just trying to display the stored content from MySql (created by CuteEditor) in a regular webpage - I'm not retrieving it for viewing within CuteEditor. I'm seeing html like <br>, etc.
     
    I haven't even tried to bring the stored data back into CuteEditor for actual editing, yet...
     
  •  09-08-2010, 7:56 AM 63889 in reply to 63877

    Re: saved html in mysql db displays as plain text when retrieved

    Dear mp5802,
     
    You can retrieve these html code from database, and then load it to cuteeditor instead of "TextBox". If you assign the html code to $editor->Text, it will display the original text at "Normal" view and display the html code at "HTML" view in CuteEditor for PHP, the following is the usage of this property: 
    $editor->Text="<p><span style='color:#cc0000;'><b>This paragraph will be edited by the first instance...</b> </span></p>";
    If you search "$editor->Text" in cuteeditor for php download package, you can find lots of related examples.
     
     
    Thanks for asking

                  
        
  •  09-08-2010, 11:01 AM 63894 in reply to 63889

    Re: saved html in mysql db displays as plain text when retrieved

    Eric -
     
    I've now got my add, edit, and display webpages working in my notes application working with CuteEditor integration. My legacy notes work, too, but they are all run together sentences. I can live with that. I'm able to edit them with CuteEditor, so I can touch them up and make them look good now with all the features in CuteEditor.
     
    Thanks for your help.
     
    - Mike
  •  09-08-2010, 1:10 PM 63898 in reply to 63894

    Re: saved html in mysql db displays as plain text when retrieved

    Glad to know it works.
     
    Thank you for asking
View as RSS news feed in XML