ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

[SOLVED] TinyMCE and Form set_value problem

January 14, 2010 9:03am

Subscribe [4]
  • #1 / Jan 14, 2010 9:03am

    jjmax

    9 posts

    Hi,

    I’m using TinyMCE on a site I’m building.
    I want the user to be able to add articles to the site. To this end I’ve setup a form with some validations.
    If the validation returns false I want to alert the user, but also want to repopulate the form fields with the data they’ve already added.
    This works fine using the set_value() function of the form helper.
    The only thing is that when the form is reloaded and the fields repopulated the TinyMCE fields are not being formatted properly.

    For example:
    If I type the following into a textarea with TinyMCE enabled -

    How
    Now
    Brown
    Cow

    TinyMCE writes this html code -

    How
    Now
    Brown
    Cow

    But once the form gets repopulated the TinyMCE textarea shows this -

    How
    Now
    Brown
    Cow

    With the generated html being - (I’ve added spaces around each &)

    & lt;p & gt;How & lt;br / & gt;Now & lt;br / & gt;Brown & lt;br / & gt;Cow & lt;/p & gt;


    Can anyone tell me why this is happening and how I can fix it please?

    Thanks for your time

  • #2 / Jan 14, 2010 10:19am

    flaky

    291 posts

    I’m not very sure of this
    but if you are inserting the data with Active Record, AR escapes converts html tags to text for security.

  • #3 / Jan 14, 2010 10:31am

    Maglok

    402 posts

    Can you post a little more mabye?

    Got TinyMCE working on a lot of my apps, is very usefull. The active record class is giving me no problems if I just use the ->input().

    My TinyMCE is setup like this:

    [removed]
    tinyMCE.init({
        mode : "specific_textareas",
        editor_selector : "editor",
        plugins : "spellchecker",
        theme : "advanced",
        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,|,cut,copy,paste,|,undo,redo,|,bullist,numlist,blockquote,|,outdent,indent,|,hr,image,spellchecker",
        theme_advanced_buttons2: "",
        theme_advanced_buttons3: "",
        spellchecker_languages : "+Dutch=nl,English=en",
        content_css : "<?php echo base_url(); ?>css/something.css",
        save_enablewhendirty : true
    });
    [removed]

    You kinda have to be doing something with the data before you post it. Perhaps like escaping indeed.

  • #4 / Jan 14, 2010 10:31am

    flaky

    291 posts

  • #5 / Jan 14, 2010 10:50am

    jjmax

    9 posts

    Hi Lads,

    Thanks for the replies.

    @flaky
    That last link seems to be pretty much what’s happening with me too
    I’m new to CI, is AR getting involved when I use the Form helper set_value function?

    @Maglok
    Thanks for your input I’ll try your init on my page.
    I’ve setup TinyMCE to edit html content that I’ve stored in the DB, the only difference here is I’m using set_value as opposed to pulling the data from the DB. I want to prevent my users from creating an articel with the same title as another one, but when I send them back to the article page I want to repopulate the form fields with the data they’ve already added. Here’s the View code -

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html >
    
    <head>
            ...
        
        [removed][removed]
        [removed]
            tinyMCE.init({
                mode : "exact",
                elements: "main_content",
                plugins : "phpimage,fullscreen",
                relative_urls : false,
                remove_script_host : true,
                document_base_url : "http://localhost/raplom-cms/",
                theme_advanced_buttons1 : "bold,italic,underline,strikethrough,separator,justifyleft,justifyright,justifycenter,justifyfull,separator,bullist, numlist,outdent,indent,separator,cut,copy,paste,separator,unlink,link,separator,phpimage,separator,formatselect,code,fullscreen",
                theme_advanced_blockformats : "h2,h3,h4,blockquote,dt,dd",
                theme_advanced_toolbar_align : "left",
                theme_advanced_buttons2 : "",
                theme_advanced_buttons3 : "",
                theme_advanced_toolbar_location : "top",
                theme : "advanced"
            });
        [removed]
            ...
        
    </head>
    
    <body>
    <?php
            ...
            
            echo form_open("article/add_article");
            
            echo form_label('Title', 'title');
            echo form_input('title');
            
            echo form_label('Body', 'content');
            $data = array(
                'name'        => 'content',
                'id'        => 'main_content',
                'value'        => set_value('content'),
                'rows'        => '30',
            );
            echo form_textarea($data);
            
            echo form_label('Meta Description', 'meta_description');
            $data = array(
                'name'        => 'meta_description',
                'id'          => 'meta_description',
                'rows'        => '5',
            );
            echo form_textarea($data);
            
            echo form_label('Meta Keywords', 'meta_keywords');
            $data = array(
                'name'        => 'meta_keywords',
                'id'          => 'meta_keywords',
                'rows'        => '5',
            );
            echo form_textarea($data);
            
            echo form_submit('add_article', 'Add Article');
            
            echo form_close();
            
            ...
    ?>
    </body>

    and here’s the Controller code -

    function add_article()
            {
                $this->load->library('form_validation');
                $this->form_validation->set_error_delimiters('', '
    ');
                
                $this->form_validation->set_rules('title', 'Title', 'required|trim');
                $this->form_validation->set_rules('content', 'Content', 'required|trim');
                $this->form_validation->set_rules('meta_description', 'Meta Description', 'trim');
                $this->form_validation->set_rules('meta_keywords', 'Meta Keywords', 'trim');
                
                if($this->form_validation->run() == TRUE)
                {
                    $this->load->model('article_model');
                    
                    $data = array(
                        'title'            => $this->input->post('title'),
                        'content'        => $this->input->post('content'),
                        'meta_description'    => $this->input->post('meta_description'),
                        'meta_keywords'        => $this->input->post('meta_keywords'),
                        'url'            => date('M-Y').'/'.url_title($this->input->post('title'), 'dash', TRUE)
                        );
                    
                    $query = $this->article_model->add_article($data);
                    
                    if($query == true)
                    {                
                        $this->session->set_flashdata('success_message', 'You have successfully added an article');
                        redirect('article', 'refresh');
                    }
                    else
                    {
                        $this->session->set_flashdata('fail_message', 'There is already another article with this title.
    Just change the article title and resubmit it.');
                        redirect('article', 'refresh');
                    }
                }
                else
                {
                    $this->save_posted_data();
                    $this->session->set_flashdata('fail_message', validation_errors());
                    $_SESSION['flashdata'] = validation_errors();
                    redirect('article', 'refresh');
                }
            }

    Thanks again for the help. I’ll try out your suggestions and let you know what happens.

  • #6 / Jan 14, 2010 10:57am

    flaky

    291 posts

    Only upon insertion or update AR does that check and modification of the data

  • #7 / Jan 14, 2010 11:37am

    jjmax

    9 posts

    Thanks for the reply flaky.

    I’ve been working on this a bit more, and when I past HTML code directly into the TinyMCE textarea it appears as the HTML code.

    So -

    howya
    <ol>
    <li>Yer</li>
    <li>a</li>
    <li>hero</li>
    </ol>

    Is shown as this in the WYSIWYG editor -

    howya
    <ol>
    <li>Yer</li>
    <li>a</li>
    <li>hero</li>
    </ol>

    And this in the html editor -

    &-lt;p&-gt;howya&-lt;/p&-gt;
    &-lt;ol&-gt;
    &-lt;li&-gt;Yer&-lt;/li&-gt;
    &-lt;li&-gt;a&-lt;/li&-gt;
    &-lt;li&-gt;hero&-lt;/li&-gt;
    &-lt;/ol&-gt;

    The dashes after the ampersands are there to stop the html entities being changed.

  • #8 / Jan 14, 2010 11:41am

    Ben Edmunds

    812 posts

    Hey John,

    In your article controller try using $_POST[‘data’] instead of $this->input->post(‘data’) or set_value(‘data’).


    If that doesn’t work please post your article/index controller code.

  • #9 / Jan 14, 2010 12:04pm

    jjmax

    9 posts

    Hi Ben,

    Thanks for the reply.

    I changed the controller to save posted data on fail, and then changed the view to use the $_POST data to repopulate the fields. This works as in the repopulation occurs, but the problem with html code still exists in the textarea.

    I can repopulate inputs and textareas that don’t have tinymce enabled on them, but repopulating a tinmce enabled field results in the html being being displayed in the WYSIWYG editor and being converted to html entities in the html editor.

    TinyMCE must have a setting somewhere to stop this happening. Funnily though when I setup the edit controller to edit existing pages, the html from the db is being displayed properly.
    Why would TinyMCE be differentiating between html pulled from the DB and html pasted straight into it or taken from $_POST?

    All the best,
    John

  • #10 / Jan 14, 2010 12:09pm

    flaky

    291 posts

    the change occurs in the Active Record not in $this->input->post(‘some_var’);
    so you should insert the data with a query
    example

    $title = $this->input->post('title');
    $body = $this->input->post('body');
    $query = "insert into table(title, body) values('$title', '$body')";
    $this->db->query($query);

    but keep in mind this isn’t very secure.

  • #11 / Jan 14, 2010 12:58pm

    Ben Edmunds

    812 posts

    flaky,
    dude read the post, this is happening before anything ever touches the database.  It’s in the form validation.


    John,
    Looks like you might have to do it with JS like so:

    var tiny = tinyMCE.get('editor1');
    tiny.setContent('HTML content that got passed through POST.');

    You can reference this post for more info: http://tinymce.moxiecode.com/punbb/viewtopic.php?id=10090

    Good luck!

  • #12 / Jan 14, 2010 1:33pm

    jjmax

    9 posts

    Ben,

    You’re a legend!
    This sounds like a good solution. Thanks for that and the link.
    I’m at home now, but will do the fix in the morning. I’ll let you know how it goes.

    All the best,
    John

  • #13 / Jan 15, 2010 7:00am

    jjmax

    9 posts

    Hey guys,

    I tried your idea Ben but wasn’t sure about getting it to work, so started trying to attack this problem form a different angle.

    Instead of using the CI form helper to create the textarea, I did this -

    echo form_label('Body', 'content');
            if (isset($_POST['content']))
            {
                $value = $_POST['content'];
            }
            else
            {
                $value = '';
            }
            echo '<textarea id="main_content" rows="30" cols="90" name="content">' . $value .'</textarea>';

    This works! :D

    Not sure how it is from a coding best practice point of view but it is getting the job done.

    Thanks again for everyone’s help. Hopefully this can help someone else.

  • #14 / Jan 15, 2010 10:21am

    Ben Edmunds

    812 posts

    Good job man!

    If you’re doing this in the view you can shorten your syntax a little like this if you want:

    <?php echo form_label('Body', 'content');?>
    <textarea id="main_content" rows="30" cols="90" name="content"><?php echo (isset($_POST['content'])) ? $_POST['content'] : '';?></textarea>

    Either should work fine though.

  • #15 / Jan 15, 2010 10:45am

    jjmax

    9 posts

    Thanks Ben!
    Will do.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases