How to read incoming object data in a PHP file

by | Jun 22, 2021 | PHP

Introduction

Whatever we see on our website, it is either saved within HTML or being fetched from a table. The HTML has a script file attached with it which calls backend, where SQL codes are written. These SQL codes interact with database. In this article we will learn how to read incoming object data in a PHP file.

Syntax

$obj = json_decode($_POST["<name of the object sent from frontend>"]);

How to read incoming object data in a PHP file

Suppose there is a use case where you need to delete a row data from table, and for that you need to send keys from frontend (JavaScript) to backend (PHP).
The object sent from frontend should be in the form of a JSON. Then this JSON is decoded in PHP and thereafter individual columns/fields are accessed as shown below:

$obj = json_decode($_POST["data"]);
    $Date = $obj->Date;
    $Type = $obj->Type;
    $Reference = $obj->Reference;
    $Received = $obj->Received;
    $ReceivedDate = $obj->ReceivedDate;
    $Mode = $obj->Mode;
    $Staff = $obj->Staff;
    $guid = $obj->guid;

Now these fields can be used to perform delete or any other operations.

Full Code:

function onDeleteTransaction(){
$servername = "localhost";
$username = "root";
$dbname = "arrowDB";
// Create connection
$conn = new mysqli($servername, $username,"", $dbname);
// Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }
    $obj = json_decode($_POST["data"]);
    $Date = $obj->Date;
    $Type = $obj->Type;
    $Reference = $obj->Reference;
    $Received = $obj->Received;
    $ReceivedDate = $obj->ReceivedDate;
    $Mode = $obj->Mode;
    $Staff = $obj->Staff;
    $guid = $obj->guid;
    $sql = "DELETE FROM transactions WHERE guid = '{$guid}' and Date = '{$Date}'and Type = '{$Type}' and Reference = '{$Reference}'and Received = '{$Received}' and ReceivedDate = '{$ReceivedDate}'and Mode = '{$Mode}' and Staff = '{$Staff}'";
    if(mysqli_query($conn,$sql)){
    $dataArray[0] = 'Deletion Successful';
    $dataArray[1] = '200';
    echo json_encode($dataArray);
    }
    else{
    $dataArray[0] = 'Deletion failed';
    $dataArray[1] = '401';
    echo json_encode($dataArray);
    }
}

Calling PHP to read incoming object data from frontend

In UI, we need to create an object and pass that object in the form of JSON using a function call as shown below:

onLogDataDelete: function (oEvent) {
    var that = this;
    this.http = "http://";
    this.uri = this.http + "localhost:/test.php";
    var sPath = this.byId("logOvpTableid").getSelectedItems()[0].getBindingContextPath();
    var obj = this.getView().getModel("logOvpModel").getProperty(sPath);
    var data = JSON.stringify({
        "Date": obj.Date,
        "Type": obj.Type,
        "Reference": obj.Reference,
        "Received": obj.Received,
        "ReceivedDate": obj.ReceivedDate,
        "Mode": obj.Mode,
        "Staff": obj.Staff,
        "guid": obj.guid
    });
    // read msg from i18n model
    var sMsg = "Do you want to delete chosen data?";
    MessageBox.confirm(sMsg, function (rValue) {
        if (rValue === "OK") {
            $.ajax({
                url: that.uri,
                type: "POST",
                data: {
                    method: "onDeleteTransaction",
                    data: data,
                },
                dataType: "json",
                success: function (response) {
                    if (response[1] === "401") {
                        MessageBox.error(response[0]);
                    } else {
                        MessageBox.success(response[0]);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    MessageBox.error("Unable to Delete Log Data")
                }
            });
        }
    });
}

 

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.