Alexey Shabramov
Alexey Shabramov

Reputation: 788

PHP and jQuery AJAX - $_POST is always empty

I am trying to output my $_POST value in the alert box with jQuery AJAX function, but the $_POST value from my PHP controller is always empty and I am always getting an empty array in the response.

I am using PHP version 5.5, jQuery version 2.2.3

(I have tried multiple variants of possible solutions for this issue, but still I can't find a correct way to fix this).

1. This is my AJAX code (I didn't made it with submit function, I am just updating some content on my web page without redirection):

$.ajax({
                method: 'POST',
                url: '../OrganizationController.php',
                dataType: "json",
                contentType: "application/json",
                data: {'myForm':$('#myForm').serialize()},
                success: function(data){
                    alert(data);
                },
                error: function(xhr, desc, err){
                    console.log(err);
                }
            });

2. There is my simple PHP controller:

<?php

include('../mysql_conn.php');

$value;
if ($_POST['myForm']) {
    $value= $_POST['myForm'];
    header('HTTP/1.1 200 OK');
} else {
    header('HTTP/1.1 500 Internal Server Error');
}

header('Content-Type: application/json');
echo json_encode($value);
...
?>

3. My simple form:

 <form id="myForm">
    <input type="text" name="name" title="Name"/>
    <input type="button" name="click">Press</input>
</form>

Controller is working. If I want to output a response with my input or some test text - everything is working and I get text in the success alert box.

I have tried different ways and saw almost all answers for similar issues.

I have tried to deleted dataType, tried to change it to text, tried to remove contentType, added an object to data like in many answers people advised, tested it with simple values like {'name':John} etc., tried to make a very simple form with one input and text-field. Nothing helped.

Question:

Can you advise for this issue or some small example with my code or with other similar code with possible solution for my problem?

Edit:

We have a very informative discussion, but unfortunately none of this solutions helped. But now we understood that in the other browsers or computes everything is working fine and the problem may be more serious (in configurations etc.).

Upvotes: 0

Views: 1737

Answers (3)

Alexey Shabramov
Alexey Shabramov

Reputation: 788

I finally found the solution!

Three days I was trying to fix this issue. I was using PHPStorm IDE v.2016.1 with it's own Build-in server.

So when I have switched to the XAMPP with it's own Apache server - the problem was solved. All is working fine in different variants, so the problem was not in my code. I found that this is the real bug in PHPStorm

To solve this issue: Just switch to another server and don't use PHPStorm p.s. even when I have changed multiple servers in my PHPStorm IDE - problem wasn't solved, so be carefull.

Upvotes: 1

Satish Saini
Satish Saini

Reputation: 2968

In your PHP file, you can only retrieve the input parameters by their name. Like, in your form, if you have email or phone number and you are sending them to your controller file then get it by their name in your controller file instead of form name.

Here is the correct code to do this:

$.ajax({
     method: 'POST',
     url: 'test.php',
     dataType: "json",
     data: $('#myForm').serialize(),
     success: function(data) {
         alert(data);
     },
     error: function(xhr, desc, err) {
         console.log(err);
     }
 });

And in your PHP file, get the values like

<?php
if ($_POST['name']) {
    $value= $_POST['name'];
} else {
    header('HTTP/1.1 500 Internal Server Error');
}

header('Content-Type: application/json');
echo json_encode($value);
?>

Here I assume that email is a field in your form.

Try this!

Edit: Just replaced POST param in PHP file because I saw that "name" is one of the fields in your form. Cheers!

P.S: contentType: application/json doesn't work sometimes. Here you can see some threads on it: Github Thread

Upvotes: 3

Rory McCrossan
Rory McCrossan

Reputation: 337743

The problem is because you're placing form-encoded data within a JSON object. Aside from being redundant, all this will do is confuse your PHP code.

To fix the problem, just send the serialised form and use $_POST to retrieve the sent fields as normal.

$.ajax({
    method: 'POST',
    url: '../OrganizationController.php',
    dataType: "json",
    data: $('#myForm').serialize(),
    success: function(data){
        alert(data);
    },
    error: function(xhr, desc, err){
        console.log(err);
    }
});
if ($_POST['input-field-name-here']) {
    // do something with the data here...

    header('HTTP/1.1 200 OK');
} else {
    header('HTTP/1.1 500 Internal Server Error');
}

Upvotes: 1

Related Questions