how to send json from html to php using ajax post method? i refered to this and this in order to get here but i have been getting null return value since, the rest of similar questions get me nowhere.
html(jquery ajax):
$('#unit1').change(function() {
var data = "";
var selectedUnit1 = this.value;
var dropDownNumber = "1";
var postData = {"selectedUnit": selectedUnit1, "unitNumber": dropDownNumber};
$.ajax({
type: "POST",
dataType: "json",
url: "ex02test.php",
data: {data:postData},
success: function(response){
alert(response.title); // to test for return value, now it returns null,
//hard-coding $unitTitle(example $unitTitle = 'hi'; in php file makes it display 'hi' so the php side is definitely working
// data = jQuery.parseJSON(response);
$('#txtHint').val(response[0].title);
},
error: function(e){
console.log(e.message);
}
});
$('#totalcost').val(valueFUnction());
});
php:
<?php
$jsonData=$_POST["data"];
$data = json_decode($jsonData);
$unitCode = $data->selectedUnit;
$dropDownNumber = $data->unitNumber;
$unitTitle = $dropDownNumber;
$unitFee = "10";
$reply->title = $unitTitle;
$reply->fee = $unitFee;
$jsonReply = json_encode($reply);
header('Content-Type: application/json');
echo $jsonReply;
?>