2

I'm trying to send data from the fields to a PHP file, but it always returns null.

Is my approach correct, or I'm missing something?

Thank in advance. my ajax


$.ajax({
url: '/GetData.php',
dataType: 'json',
type: 'POST',
data: {
a:a,
b:b,
c:c
},
contentType: 'application/json',
success: function(response){
console.log('success '+ JSON.stringify(response));
search_table =  JSON.stringify(response)

$('#mytable').html(search_table);

},
error: function(err){
console.log('error '+JSON.stringify(err));
//alert(JSON.stringify(err))
}
});


my GetData.php file

<?php
 $a = $_GET["a"];
 $b = $_GET["b"];
 $c = $_GET["c"];

$remote_url = "http://mydomain/GetDataDetails/?name=".$a."&age=".$b."&degree=".$c;
$opts = array(
'http'=>array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method'=>"GET",
)
);
$context = stream_context_create($opts);
$out=json_decode(file_get_contents($remote_url, false, $context),true);
header('Content-Type: application/json');
echo json_encode($out);
?>
9
  • 3
    You use method POST and in GetData use method GET, change like $a = $_POST["a"]; or change method in ajax method Commented Jan 11, 2020 at 8:56
  • When posting code, please make sure it's sensibly formatted. You want to make it a s easy as possible for us to read it. Commented Jan 11, 2020 at 9:07
  • You can also remove contentType: 'application/json' from the ajax request since you most likely just want to post the data in the "normal" way. Commented Jan 11, 2020 at 9:09
  • @SimoneRossaini thanks for your replay. I try what you said but still not working. Commented Jan 11, 2020 at 9:09
  • So instead of fixing the formatting of the code, you just made it worse. Please indent your code properly. it's hard to follow the flow without any indention. Commented Jan 11, 2020 at 9:10

1 Answer 1

1

Answer from comment:

  • Change from $_GET to $_POST in PHP
  • Remove contentType: 'application/json', from the Ajax request
Sign up to request clarification or add additional context in comments.

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.