Disable Textarea (with Tinymce) when loading mysql data

Asked

Viewed 340 times

0

To disable a textarea with tinymce I used the code

tinyMCE.get('id_textarea').getBody().setAttribute('contenteditable', false);

within a Javascript script, with an onclick event of a button, and it happened all right.

The problem now is this: I have 5 pages, each page with 5 textareas, all editable by tinymce, and with a "Save" button for each textarea. On the last page, I inserted a "Send" Submit button, which saves a "yes" in the mysql BD, in a table with the textarea and with the "sent" field that receives that "yes". What I’m trying to do and I can’t, is, after clicking this "Submit" Submit button, that all textarea are disabled for editing, through an SQL query to the "sent" field, that is, after saving the "yes", the user can’t edit the textarea anymore, neither at that time, nor after reopening pages with textarea.

I already inserted the script in the tag <head>, I tried to put the script in an echo inside php, and nothing helped to disable it, just save the yes in the BD. Follow the script and php/html code. Inside the tag <head>:

function enviartudo() {            
  tinyMCE.get('id_textarea1').getBody().setAttribute('contenteditable', false);            
  tinyMCE.get('id_textarea2').getBody().setAttribute('contenteditable', false);
}

PHP

After a SELECT to the BD:

include_once('conexao.php');
$query = "SELECT..........";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
if ($row['enviado'] == 'sim') {
    echo '<script type="text/javascript">
             enviartudo();
          </script>';
}

if (isset($_POST['enviar'])) {
    $query1 = "UPDATE tabela SET enviado = 'sim' ";
    $data1 = mysqli_query($dbc, $query1);
    mysqli_close($dbc);
 }

HTML

<tr>
    <td>
       <label>TEXTAREA 1</label>
       <textarea type="text" id="textarea1" name="textarea1" class="areatexto"
       maxlength="5000">
          <?php echo (isset($_POST['textarea1']) ? $_POST['textarea1'] : 
          htmlspecialchars_decode(stripslashes($row['textarea1']))); ?>
       </textarea>
       <input type="submit" name="submit1-1" value="Salvar" class="btn_save_item"/>
    </td>
</tr>
<tr>
    <td><br />
       <label>TEXTAREA 2</label>
       <textarea type="text" id="textarea2" name="textarea2" class="areatexto" maxlength="8000">
          <?php echo (isset($_POST['textarea2']) ? $_POST['textarea2'] : 
          htmlspecialchars_decode(stripslashes($row['textarea2']))); ?>
       </textarea>
       <input type="submit" name="submit1-2" value="Salvar" class="btn_save_item" />
    </td>
</tr>
<tr>
   <td>
      <input type="submit" name="enviar" value="Enviar" class="btn_save_item" />
   </td>
</tr>

I did not put all the code of the 5 pages with the 5 textarea of each one, because it would be too big, but the code is repetitive, changing only id/name of div, form, textarea, Submit buttons, among others.

  • if ($row['enviado'] == 'sim') { $desabilitado = "disabled"; and in the textareas <textarea type="text" ..... maxlength="5000" <?php echo $desabilitado ?>>

  • @Leocaracciolo, had already tried this, but the tinymce does not accept the "disabled", nor the "readonly" in the textarea. If I put readonly:1 in the tinymce setup, all textareas already start readonly when loading, and that’s not what I want. Anyway, thanks, but the problem remains..

  • Guys, I found a solution. I created another tinymce script, but changing the name of the Selector and another skin (to change the background of the textarea), and insert readonly=1. Then, in PHP, I made an if/Else, creating a variable that defines the textarea class. And in HTML code, in textarea, I inserted a class with PHP: class="<?php echo $variavel; ?>" . I’ll put the solution down..

1 answer

0


As I commented above, after a good night’s sleep I found a solution.

I created another tinymce script, but I changed the name of the Selector and inserted the readonly=1, menubar=false and left only the preview on Toolbar, all to not allow more qq editing. I created tb another skin (to change the textarea background). In PHP, I made an if/Else, creating a variable that defines the textarea class. And in the HTML code, in the textarea, I inserted a class with PHP. They follow the codes:

Script 1 of tinymce:

tinymce.init ({
        selector: '.areatexto',
        height: 200,
        skin: 'custom',
        theme: 'modern',
        resize: false,
        plugins: 'print charwordcount preview searchreplace autolink visualblocks visualchars image link media table charmap hr pagebreak nonbreaking anchor insertdatetime advlist lists textcolor imagetools contextmenu colorpicker textpattern',
        toolbar1: 'formatselect | bold italic strikethrough forecolor backcolor | link | alignleft aligncenter alignright alignjustify  | numlist bullist outdent indent  | removeformat | table image | preview',});

I created another tinymce script:

tinymce.init ({
        selector: '.areatexto2', //aplica o plugin em todos textarea com class="areatexto2"
        height: 200, //altura de cada textarea
        skin: 'custom2',
        theme: 'modern',
        readonly: 1,
        resize: false,
        plugins: 'preview',
        toolbar1: 'preview',
        menubar: false, });

I created an if/Else in PHP:

if ($row['enviado'] == 'sim') { $classTextarea = "areatexto2"; } else { $classTextarea = "areatexto"; }

And in HTML:

<textarea type="text" id="xxxxxx" name="xxxxx" class="<?php echo $classTextarea; ?>" maxlength="5000">

Works perfectly, no longer allowing editing of textarea (with class="areatexto2"), because readonly=1, in addition to removing the menubar and all editing plugins.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.