This is my AJAX request
$(document).ready(function(){
$("#register-submit").click(function(){
var formdata = {hotelname: $('#hotelName').val(), contactType: $('#contactType').val(), contactNumber: $('#contactNumber').val(), addrOne: $('#addrLineOne').val(), addrTwo: $('#addrLineTwo').val(),cityName: $('#cityName').val(), stateName: $('#stateName').val(),localityName: $('#localityName').val(), pincode: $('#pincode').val(), managerName: $('#mngrName').val(), managerEmail: $('#mngrEmail').val(), managerPhone: $('#mngrPhone').val()}
jQuery.ajax({
url: "../api/v1/admin/ch_partialBusinessRegister.php",
data: JSON.stringify(formdata),
type: "POST",
async: false,
success: function(res) {
var result = res.Result;
if(result.success === true)
{
alert("Random Password is Generated : "+res.Result.password);
window.location.reload();
}
else
alert("Registration Failed. "+res.Result.msg);
}
});
});
});
In my ch_partialBusinessRegister.php
$inputJson = file_get_contents('php://input');
$post_vars = json_decode($inputJson, true);
$businessName = $post_vars['hotelname'];
$address1 = $post_vars['addrOne'];
$address2 = $post_vars['addrTwo'];
$locality = $post_vars['localityName'];
$city = $post_vars['cityName'];
$state = $post_vars['stateName'];
$zip = $post_vars['pincode'];
$mName = $post_vars['managerName'];
$mEmail = $post_vars['managerEmail'];
$mPhone = $post_vars['managerPhone'];
Am able to get the data and pass it in the POST parameters successfully. But when I change the formdata as
$(document).ready(function(){
$("#register-submit").click(function(){
var formdata = $('form').serializeArray();
jQuery.ajax({
url: "../api/v1/admin/ch_partialBusinessRegister.php",
data: JSON.stringify(formdata),
type: "POST",
async: false,
success: function(res) {
var result = res.Result;
if(result.success === true)
{
alert("Random Password is Generated : "+res.Result.password);
window.location.reload();
}
else
alert("Registration Failed. "+res.Result.msg);
}
});
});
});
and then JSON.stringify it, it is sending the variables through POST parameters like this
[{"name":"hotelName","value":"test"},{"name":"contactType","value":"LandLine"},{"name":"contactNumber[]","value":""},{"name":"AddrOne","value":"test"},{"name":"addrTwo","value":"test"},{"name":"pincode","value":"test"},{"name":"mngrName","value":"test"},{"name":"mngrEmail","value":"test"},{"name":"mngrPhone","value":"test"}]
But in PHP it is not reading the values.
Same way if I use
var mydata = $('form').serialize();
data: JSON.stringify(mydata)
It Posts this data, but not read by PHP
"hotelName=&contactType=LandLine&contactNumber%5B%5D=&AddrOne=&addrTwo=&pincode=&mngrName=test&mngrEmail=test&mngrPhone=test"
$('form').serialize();???serializeArray()so why not instead useserialize()and use relevant name attribute for each input?hotelName!=hotelnamestart to fix it and the same for all your attributesname