First things first, an explanation of what i'm doing: I'm making a simple form with an ajax request (on a form submit button) that updates company information, I'm fairly new to php but want to use a data transfer object in the ajax (like I've done before in other languages) but i'm not getting far
OK so this is probably a really simple question. Like i said above i've used AJAX Data transfer objects before, just not with php... I'm getting a little fustrated as i can't seem to pick the object data out to use in my sql update on the target page. I can see in the console.log that the object seems fully formed and contains the data, but i can't for the life of me get the syntax right to pick the data out! Help
Ajax Request:
var $CompanyName = $("#CompanyName").val(),
$TelNumberAreaCode = $("#TelNumberAreaCode").val(),
$TelNumber = $("#TelNumber").val(),
$EmailAddress = $("#EmailAddress").val(),
$AddressLine1 = $("#AddressLine1").val(),
$AddressLine2 = $("#AddressLine2").val(),
$AddressCity = $("#AddressCity").val(),
$AddressCounty = $("#AddressCounty").val(),
$AddressPostCode = $("#AddressPostCode").val();
var CompanyDetailsDTO = {};
CompanyDetailsDTO.CompanyName = $CompanyName;
CompanyDetailsDTO.TelNumberAreaCode = $TelNumberAreaCode;
CompanyDetailsDTO.TelNumber = $TelNumber;
CompanyDetailsDTO.EmailAddress = $EmailAddress;
CompanyDetailsDTO.AddressLine1 = $AddressLine1;
CompanyDetailsDTO.AddressLine2 = $AddressLine2;
CompanyDetailsDTO.AddressCity = $AddressCity;
CompanyDetailsDTO.AddressCounty = $AddressCounty;
CompanyDetailsDTO.AddressPostCode = $AddressPostCo;
var DTO = { 'request': CompanyDetailsDTO }
$.ajax({
type: "POST",
url: "php/updates/update-businessdetails.php",
data: JSON.stringify(DTO),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: CompanyDetailsSuccess,
error: CompanyDetailsError
});
PHP Update / Target File Code:
<?php
// DB Connection
// How can i pick the below vars out the object???
// I've tried loads of different syntaxes that i think should/could be right
$CompanyName = ??
$TelNumberAreaCode = ??
$TelNumber = ??
$EmailAddress = ??
$AddressLine1 = ??
$AddressLine2 = ??
$AddressCity = ??
$AddressCounty = ??
$AddressPostCo = ??
$sql="UPDATE `businessdetails` SET `TelNumberAreaCode` = '$TelNumberAreaCode',`EmailAddress` = '$EmailAddress',`AddressLine1` = '$AddressLine1',`AddressLine2` = '$AddressLine2',`AddressCity` = '$AddressCity',`AddressCounty` = '$AddressCounty',`TelNumber` = '$TelNumber',`AddressPostCode` = '$AddressPostCode',`CompanyName` = '$CompanyName'";
$result = $conn->query($sql);
if ($result)
{
header('Content-Type: application/json');
print json_encode($result);
}
?>
I must be doing something wrong, and or stupid as i can't seem to find the answer either?!