I want to get specific value from server side. Therefore I am using $http to pass variable from front-end (Angularjs javascript) to backend(php). After server side (php) get the value from front-end. It will run sql query to get data from mysql and return the data to front-end. However, in my console of front-end, it shows undefined error. The following is my code:
Front-end Javascript
$http({
method: "post",
url: "http://localhost/php/UpdateLastLogin.php",
data: {
user_name: user_name,
CurrentTimestamp: CurrentTimestamp
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(response) {
console.log(response);
}).error(function(response) {
console.log(response);
});
Backend PHP
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With");
header('P3P: CP="CAO PSA OUR"');
header("Content-Type: application/json; charset=utf-8");
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
@$CurrentTimestamp = $request->CurrentTimestamp;
@$user_name = $request->user_name;
$servername = "localhost";
$username = "jack";
$password = "1234";
$dbname = "daikinchat";
$CurrentTimestamp = '"' . $CurrentTimestamp . '"';
//$user_name = '"' . $user_name . '"';
$user_name = "Lisa Wong";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT last_login_timestamp FROM user_directory WHERE username = '$user_name'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_row($result);
echo $row[0];
?>
application/json
in your $http request – Divyesh Savaliya Oct 10 at 7:21