0

I am posting form data from create.php as below using $.ajax.

$(document).ready(function () {

    var request;
    $("#create-pto").submit(function(e) {
        // Abort any pending request
        if (request) {
            request.abort();
        }

        var $form = $(this);
        var serializedData = $form.serialize();

        request = $.ajax({
            url: 'index.php',
            type: 'POST',
            contentType : 'application/json',
            data:  serializedData
        }).done(function (response, textStatus, jqXHR) {
            // Log a message to the console
            console.log("Hooray, it worked!");
        });

        e.preventDefault();

    });

});

I can see the posted data and i can log post success.

But in index.php could not retrieve posted data.

This is what i am trying in index.php

var_dump($_POST);  

Form :

       <form class="form-horizontal"  id="create-pto"  >
                 <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-   label">App ID</label>

                    <div class="col-sm-4">
                        <input type="text" class="form-control" name="app-id" id="app-id" placeholder="App ID">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputPassword3" class="col-sm-2 control-label">App Name</label>

                    <div class="col-sm-4">
                        <input type="text" class="form-control" name="app-name" id="app-name"
                               placeholder="App Name">
                    </div>
                </div>

                <div class="form-group">
                    <label for="inputPassword3" class="col-sm-2 control-label">Instance Name</label>

                    <div class="col-sm-4">
                        <input type="text" class="form-control" id="inputPassword3" name="ins-name"
                               placeholder="Instance Name">
                    </div>
                </div>

            <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="create" class="btn btn-success">Create PTO</button>
                    </div>
                </div>
              </form> 

Pls help.

12
  • your form code please? Commented Jun 22, 2016 at 9:46
  • When you watch your "network" with the web tool brower, is it ok? Commented Jun 22, 2016 at 9:49
  • #create-pto is form id or button id ? Commented Jun 22, 2016 at 9:57
  • #create-pto is form id Commented Jun 22, 2016 at 10:22
  • When form will submit ,without any submit button ? Can you post your form code Commented Jun 22, 2016 at 10:27

4 Answers 4

1

Try changing the contentType on your JavaScript code:

contentType : 'application/x-www-form-urlencoded',

And if you want to keep the contentType to json, try this way on your PHP file:

echo file_get_contents('php://input');

Sign up to request clarification or add additional context in comments.

3 Comments

You should use JSON.stringify(serializedData) to send json type data. and try to catch them using $_REQUEST.
Did u try with var_dump($_REQUEST)
I tried contentType : 'application/x-www-form-urlencoded' , data:serializedData and var_dump($_REQUEST) I get array(0) { } in index.php for var_dump($_REQUEST);
1

If you use .serialize() you need to change contentType to 'application/x-www-form-urlencoded'.

If you want to keep contentType: 'application/json' you have to use JSON.stringify instead.

Also note that JSON strings in POST requests are retreived using file_get_contents('php://input'); not $_POST

2 Comments

i changed contentType: to 'application/x-www-form-urlencoded'. and tried file_get_contents('php://input'); but echoing doesnt print anything on index.php
@Spdexter Try not serializing or stringifying the data at all and check if $_POST gets populated
1

Change your

contentType : 'application/json'

to

dataType: "JSON"

It will start work.. I tested code on my system.

Comments

0

try type:GET instead of type:POST,also call var_dump($_GET) in index.php

Comments

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.