0

I want to POST my form using PHP, but for some reason seems impossible, whatever I try to post the message is always empty.

I tried this code:

HTML

<div class="message-container">
    <form id="imageForm" name="form" action="/requests/post_message.php" method="POST" enctype="multipart/form-data">
        <div class="message-form-content">
            <div class="message-form-header">
                <div class="message-form-user"></div>
                <div class="message-form-private"></div>
                <div class="message-loader" id="post-loader" style="visibility: hidden"><div class="preloader"></div></div>
            </div>

            <div class="message-form-inner">
                <textarea id="post" class="message-form" placeholder="message_form" name="message"></textarea>
            </div>

            <div class="selected-files"><span id="queued-files">0</span> Files selected</div>
            <div class="message-form-input"><input type="text" name="value" id="form-value"></div>

            <div type="button" name="action" class="message-btn button-active" value="Post"><a onclick="startUpload()">Post</a></div>

        </div>
        <iframe id="my_iframe" name="my_iframe" src="" style="display: none"></iframe>
    </form>
</div>

JavaScript

function startUpload() {
    document.getElementById("imageForm").target = "my_iframe";
    document.getElementById("imageForm").submit();
    alert($("#imageForm").serialize()); //for debug
    document.getElementById("post-loader").style.visibility = "visible";
}

post_message.php

<?php
include("../includes/config.php");
session_start();

print_r($_POST['message']);

From network console I double checked the request and it's correct, I can see POST with status 200.

However when I write into the textarea and click on the Post button, the JS alert show me the message correctly instead the print_r of my PHP is empty, what's wrong with my form?

6
  • You are sending a GET, and checking for POST variables. Check $_GET instead of $_POST. Commented Jan 3, 2017 at 11:38
  • alert($("#imageForm").serialize()); //for debug ? Commented Jan 3, 2017 at 11:39
  • @VladimirM I saw this stackoverflow.com/a/7667608/4458531 Commented Jan 3, 2017 at 11:39
  • 1
    @NineCattoRules what I was hinting is that your id reference is missing the hash tag... Commented Jan 3, 2017 at 11:41
  • Do you have jquery included? Commented Jan 3, 2017 at 11:41

1 Answer 1

1

Answer was accepted before it actually resolved the question. I don't know exactly where was the problem located in the end. Originally the asker said the request was being sent by GET according to his browser inspector, but later retracted it.

I only found that this was wrong:

alert($("imageForm").serialize()); //for debug

should be:

 alert($("#imageForm").serialize()); //for debug

and better still:

 console.log($("#imageForm").serialize()); //for debug
Sign up to request clarification or add additional context in comments.

7 Comments

Ok, but what about this? <form id="imageForm" name="form" action="/requests/post_message.php" method="POST" enctype="multipart/form-data">
Sorry, you are right about that. But you said that in the console you see a GET request with status 200...
a POST request for the page post_message.php when I click on POST button
Then as I said, you are sending it via GET, not via POST. Try checking $_GET and see what you get.
Do a print_r($_POST), and post what you get, please
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.