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.).
contentType: "application/json",method: 'POST',--->type :'post'