0

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?!

Satpal
  • 132,252
  • 13
  • 159
  • 168
s.fray
  • 5
  • 2

1 Answers1

0

Try to use json_decode():

// @var stdClass 
$decoded_data = json_decode(file_get_contents('php://input'), false);

// And access its properties:
$CompanyName = $decoded_data->request->CompanyName;
$TelNumberAreaCode = $decoded_data->request->TelNumberAreaCode;
// ...
Joe Black
  • 867
  • 1
  • 9
  • 10
  • This worked for me, thank you very much, i wasn't expecting the php://input but after seeing your answer and reading [link](http://stackoverflow.com/questions/8893574/php-php-input-vs-post) i understand why my trials with $_POST weren't working – s.fray May 04 '17 at 21:57
  • I'm glad it helped. – Joe Black May 04 '17 at 22:28