0

first of all i'm new here so if I do mistakes pls let me know^^"

I try to get the content of a div element with php $_POST and I dont really know how to do it. I use MVC patter if that matters. My div is editable and the answer box to write something on the page.

<form action="index.php?page=addPost&topic_id={TOPIC_ID}" method="post">
     <div id="editor" name="editor">
        Lorem Ipsum...
     </div>
     <input type="submit" name="submit" value="senden" >
</form>

I can't just use a textarea but I would like to get everything between the div element with something like

$text = $_POST['editor'];

is that possible?

Robin Schambach
  • 617
  • 3
  • 11
  • 26
  • You need AJAX way - JQuery to submit the editor as $('editor').value – Thamilhan Nov 25 '15 at 12:42
  • A div is not a form element, so you would need to use some javascript. Are you using any js libraries at present (eg jquery)? – Steve Nov 25 '15 at 12:42
  • Or just use a `textarea` form element – Steve Nov 25 '15 at 12:42
  • If theres some reason you cant use a textarea element than: whenever the div is edited, use javascript to place the content into a `` –  Nov 25 '15 at 12:43
  • I never used Ajax or jquery since now^^" So I have no idea how to it. But thank you, seems like I have to learn it. – Robin Schambach Nov 25 '15 at 12:44
  • @Fred-ii- Not sure that is a duplicate TBH – Steve Nov 25 '15 at 12:45
  • The reason why I cant use textarea is: is use this as the editor https://mindmup.github.io/bootstrap-wysiwyg/ And i searched few hours to find this template – Robin Schambach Nov 25 '15 at 12:48
  • @Fred-ii- sounds like he's trying to submit the content of the div as part of the form, not display what was submitted by the form in a div. In any case, up to the OP to clarify at this point –  Nov 25 '15 at 12:49
  • reopened, y'all can submit answers. Good luck to one 'n all ;-) – Funk Forty Niner Nov 25 '15 at 12:51
  • I saw that post and searched before I created my question: I want to get the content from a div, not load the content into a div. What I want is just a good method to implement an answer panel for my page. I could just use a blank textarea, but users should have options to modify the text as they can in forums. But I didnt find any templates for that and not really good in JS, Ajax or Jquery. – Robin Schambach Nov 25 '15 at 12:57

3 Answers3

1

Use ajax or just use a textarea form element

<form id="data" method="post" enctype="multipart/form-data">
    <div id="editor" name="editor">
        Lorem Ipsum...
    </div>
    <input type="submit" name="submit" value="senden" >
    <input type="hidden" id="topicId" value="{TOPIC_ID}" >
</form>

<script>
    $(document).ready(function(){
    $("form#data").submit(function(){
        var formData = new FormData($(this)[0]);
        formData.append('editor', $('#editor').html());

        $.ajax({
            url: 'index.php?page=addPost&topic_id='+$('$topicId').val(),
            type: 'POST',
            data: formData,
            async: false,
            success: function (data) {
                     alert(data);
                     location.reload();
            },
            cache: false,
            contentType: false,
            processData: false
            });
            return false;
        });
    });
</script>
Pistachio
  • 1,652
  • 1
  • 18
  • 38
Ninju
  • 2,522
  • 2
  • 15
  • 21
1

You could use a hidden textarea, and add the contents of the div to the textarea on form submit:

<form action="index.php?page=addPost&topic_id={TOPIC_ID}" method="post" onsubmit="getEditorContents(this);">
     <div id="editor">
        Lorem Ipsum...
     </div>
     <textarea style="display:none;" name="editor"><!-- --></textarea>
     <input type="submit" name="submit" value="senden" >
</form>

<script>
function getEditorContents(form){
    var html = document.getElementById("editor").innerHTML;
    form.editor.value = html;
    return true;
}
</script>
Steve
  • 20,703
  • 5
  • 41
  • 67
0

Here is how you can proceed with:

<script>
    $(document).ready(function(){
        $("#myform").on("submit",function(e){
            e.preventDefault();  //Prevent default form submission
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    if (xmlhttp.responseText == "true") {
                        //success message
                    } else {
                        //error message
                    }
                }
            }
            xmlhttp.open("POST", "index.php", true);
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  //to maintain HTML format though you pass value through POST
            xmlhttp.send("page=addPost&topic_id="+$('#topicID').val()+"value=" + $('#editor').html());
        });
    });
</script>

Small rework with your form:

<form id="myform" method="post">
     <div id="editor" name="editor">
        Lorem Ipsum... in WYSIWYG format
     </div>
     <input type="hidden" id="topicID" value="{TOPIC_ID}"/>
     <input type="submit" name="submit" value="senden" />
</form>
Thamilhan
  • 13,040
  • 5
  • 37
  • 59
  • Why the long hand ajax request if you are using jquery? why not `$.post($(this).attr('action'), {editor: $('#editor').html()}, function(){...});` – Steve Nov 25 '15 at 12:58
  • I prefer this way, since personally I feel better. By the way, this way helps the beginner know what the actual processing is done – Thamilhan Nov 25 '15 at 13:00