Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post $_POST variable is empty when MIME type JSON is used

    Below is an excerpt from my pet project.
    Code:
    //JS
        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                console.log(xmlhttp.responseText);
            }
        }
        var mac_data=new Object();
        mac_data.ipaddress_eth0=document.getElementById('ipnic0_txt').value;
        mac_data.owner=document.getElementById('owner_txt').value;
        var json_data=JSON.stringify(mac_data);
        xmlhttp.open("POST","php/addMachine.php",true);
        xmlhttp.setRequestHeader('Content-Type', 'application/json')
        xmlhttp.send(json_data);
    
    //PHP
    var_dump ($_POST);
    header("Content-Type: application/json");
    The value of $_POST (as seen in firebug) is "array(0) {}".
    I am unable to figure out the problem with this piece of code.
    Please guide me.

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Are they on the same page? PHP and Javascript don't talk to each other. If they're on the same page, the PHP POST array won't have anything in it, as it won't be populated on the server - as the javascript hasn't run yet because it runs on the client side.

    To clarify. PHP is executed by the server, so the server will parse all the headers sent from the browser, fill the relevant information out into the relevant constants and arrays, then run the PHP script. The script then parses and outputs the script, and included in the output should be the javascript. That means the javascript is run after the PHP starts outputting - after PHP has finished doing it's thang.

    Have a rethink about your logic .
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post @BluePanther

    When you ask me whether they are on the same page, do you mean same file?
    Let me clarify on the way my code is organized. Below is the directory structure


    ls /var/ww/html
    css data exe FirePHPCore-0.3.2 index.html js json php
    ls /var/www/html/js
    addMac.js
    ls /var/www/html/php
    addMachine.php


    Few lines from index.html (not the entire page).
    There is a button on the page too which listens to Click event and calls the JS function.
    Code:
                    </div>
                        <span id="ipnic0_label">IP Address NIC 0</span>
                        <input type="text" id="ipnic0_txt" />
                    </div>
                    <div>
                        <span id="owner_label">Owner</span>
                        <input type="text" id="owner_txt" />
                    </div>
    Few lines from addMac.js (not the entire page)
    Code:
        var mac_data=new Object();
        mac_data.ipaddress_eth0=document.getElementById('ipnic0_txt').value;
        mac_data.owner=document.getElementById('owner_txt').value;
        var json_data=JSON.stringify(mac_data);
        console.log(json_data);
        xmlhttp.open("POST","php/addMachine.php",true);
        xmlhttp.setRequestHeader('Content-Type', 'application/json')
        xmlhttp.send(json_data);
    Few lines from addMachine.php (not the entire page)
    Code:
    var_dump ($_POST);
    header("Content-Type: application/json");
    I am able to make this work using a "get" request. But the POST is not working for me.
    Last edited by nayakswadesh; 12-27-2011 at 04:31 AM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •