Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass content in TinyMCE's textarea to a PHP file directly instead of sending via AJAX?

I build a simple webpage with embeded TinyMCE.

The HTML part:

<form method="post" action="dump.php">
<div id="main">
<div id="container_left">
        <textarea id="mytextarea"></textarea>
        <input id="submit" type="submit" value="submit">            
</div>
<div id="show_right"></div>
</div>
</form>

The JavaScript part.

<script src="tinymce/tinymce.min.js"></script>
<script type="text/javascript">
tinymce.init({
    selector: "textarea",
    height: 250,
    plugins: [
        "code advlist autolink lists link image charmap print preview anchor",
        "searchreplace visualblocks code fullscreen",
        "insertdatetime media table contextmenu paste"
    ],
    toolbar: "code insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
    setup: function (editor) {
        editor.on('init keydown change', function (e) {
           document.getElementById('show_right').innerText = editor.getContent();
        });
    }
});
</script>

Now I want to print content in the TinyMCE's editor, after you click the submit button.

The dump.php

<?php
    print_r($_POST);
?>

Why nothing print by dump.php?
I can send info to the dump.php via AJAX method.
1.Remove <form method="post" action="dump.php"></form> in html part.
2.Add the following js code in the JavaScript part.

<script>
function send(){
    var data = tinyMCE.get('mytextarea').getContent();
    var ajax = new XMLHttpRequest();
    ajax.open('POST','dump.php',false);
    ajax.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    ajax.send('data='+data);    
} 
ob = document.getElementById("submit");
ob.addEventListener("click",send);
</script>

I have tried that it can pass content which I type in the textarea whose id is mytextarea to dump.php and dump.php can print the array properly.

My issue is how to pass content in TinyMCE's textarea to a PHP file directly instead of sending it via AJAX? Do the work without AJAX.

like image 442
showkey Avatar asked Jan 03 '19 07:01

showkey


1 Answers

Yes, you can send it without AJAX with an hidden iframe which has as target attribute the name value from iframe. The original page will not reload. The answer from your server will be loaded in hidden iframe. In that case, you have to set the iframe display to none.

<iframe name="server_answer" style="display:none"></iframe>
<form method="post" action="dump.php" target="server_answer">
    <div id="main">
    <div id="container_left">
        <textarea id="mytextarea" name="mytext"></textarea>
        <input id="submit" type="submit" value="submit">            
    </div>
    <div id="show_right"></div>
    </div>
</form>

For your textarea you have also to write some name as attribute, for example like name="mytext" because on the server side you want get the text, which will sended. And because you did not write this name you can not get it. In your AJAX solution you do it with ajax.send('data='+data);. In this case data is like the name of your textarea.

For your PHP script:

print($_POST["mytext"]); //mytext is name of textarea

Alternative solution

You could also display the submit form as a separate page inside the iframe, and when it gets submitted then the outer page will not reload. This solution does not use AJAX too.

like image 112
Bharata Avatar answered Nov 03 '22 23:11

Bharata