Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] TinyMCE and Form set_value problem
#1

[eluser]jjmax[/eluser]
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 -
Code:
How
Now
Brown
Cow
TinyMCE writes this html code -
Code:
<p>How<br />Now<br />Brown<br />Cow</p>
But once the form gets repopulated the TinyMCE textarea shows this -
Code:
<p>How<br />Now<br />Brown<br />Cow</p>
With the generated html being - (I've added spaces around each &)
Code:
<p>& lt;p & gt;How & lt;br / & gt;Now & lt;br / & gt;Brown & lt;br / & gt;Cow & lt;/p & gt;</p>


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

Thanks for your time
#2

[eluser]flaky[/eluser]
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

[eluser]Maglok[/eluser]
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:

Code:
[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 : "&lt;?php echo base_url(); ?&gt;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

[eluser]flaky[/eluser]
check this post
http://ellislab.com/forums/viewthread/139783/
#5

[eluser]jjmax[/eluser]
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 -
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

&lt;html &gt;

&lt;head&gt;
        ...
    
    [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]
        ...
    
&lt;/head&gt;

&lt;body&gt;
&lt;?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();
        
        ...
?&gt;
&lt;/body&gt;
and here's the Controller code -
Code:
function add_article()
        {
            $this->load->library('form_validation');
            $this->form_validation->set_error_delimiters('', '<br />');
            
            $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.<br />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

[eluser]flaky[/eluser]
Only upon insertion or update AR does that check and modification of the data
#7

[eluser]jjmax[/eluser]
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 -
Code:
<p>howya</p>
<ol>
<li>Yer</li>
<li>a</li>
<li>hero</li>
</ol>

Is shown as this in the WYSIWYG editor -

Code:
<p>howya</p>
<ol>
<li>Yer</li>
<li>a</li>
<li>hero</li>
</ol>

And this in the html editor -

Code:
<p>&-lt;p&-gt;howya&-lt;/p&-gt;<br />&-lt;ol&-gt;<br />&-lt;li&-gt;Yer&-lt;/li&-gt;<br />&-lt;li&-gt;a&-lt;/li&-gt;<br />&-lt;li&-gt;hero&-lt;/li&-gt;<br />&-lt;/ol&-gt;</p>

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

[eluser]Ben Edmunds[/eluser]
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

[eluser]jjmax[/eluser]
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

[eluser]flaky[/eluser]
the change occurs in the Active Record not in $this->input->post('some_var');
so you should insert the data with a query
example
Code:
$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.




Theme © iAndrew 2016 - Forum software by © MyBB