Polymer iron-form post method not working $_POST variable empty

Asked By: user4881941

when i use post method iron-form does not work but get method works.
Here is code:

test.php:

<script>
    document.addEventListener("WebComponentsReady",function() {
        document.querySelector("#form").addEventListener('iron-form-submit',function(e){
            });
        document.querySelector("#form").addEventListener('iron-form-response',function(e){
            console.log(e);
        });
        document.querySelector("#form").addEventListener('iron-form-error',function(e,s,d){
            console.log(e);
            alert('iron form error!');
        });
    });
</script>
<body>
    <form is="iron-form" id="form" method="post" action="/user/put">
        <input name="address" required>
        <input type="submit" value="submit" >
    </form>
</body>

index.php:

<?php
    var_dump($_POST);
?>

when i submit form screenshot of headers innetwork tab:
Screenshot of headers in network tab when form submit

response tab:

array(0) {
}

Solution

Answered By: Anonymous

Maybe this is because using iron-form POST datas are sent in JSON format, look at the browser developper tools Params tab to see what is sent !

var_dump($_POST) in php script return an empty string if not decoded.

Try file_get_contents with php://input to access I/O stream and json_decode on server side, like this:

Server side php script:

<?php
$datas = file_get_contents("php://input");
$_POST = json_decode($datas, true);

$return = '<ul>';
foreach($_POST as $index=>$val) {
    $return .= '<li>'.$index.'/'.$val.'</li>';
}
$return .= '</ul>';

echo '{"my_return": "'.$return.'"}';

And the Polymer component:

<dom-module id="my-form">
    <template>
        <div class="horizontal center-center layout">
            <div>
                <div class="horizontal-section">
                    <form is="iron-form" id="formGet" method="post" action="add.php">
                        <paper-input name="name" label="Name" value="John Doe" required></paper-input>
                        <paper-input name="age" label="Age" value="97" required></paper-input>
                        <br><br><br>
                        <paper-button raised onclick="clickHandler(event)">Submit</paper-button>
                    </form>
                </div>
            </div>
        </div>
    </template>
    <script>

        function clickHandler(event) {
            Polymer.dom(event).localTarget.parentElement.submit();
        }

        Polymer({
            is: 'my-form',
            listeners: {
                'iron-form-response': 'formResponse',
                'iron-form-submit': 'formSubmit',
                'iron-form-error': 'formError'
            },
            formError: function(e) {
                alert(e.detail.error);
        },
        formSubmit: function(e) {
            //alert(document.getElementById("formGet").serialize().name);
            //alert(document.getElementById("formGet").serialize().age);
        },
        formResponse: function(e) {
             document.getElementById('contentECLP').innerHTML = e.detail.my_return;
       }
    });
</script>
techinplanet staff

techinplanet staff


Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno