-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess.php
65 lines (53 loc) · 1.72 KB
/
process.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
//set time zone
date_default_timezone_set("Asia/Colombo");
$dateAndTime = date('Y-m-d h:i:sa');
$ipAddress = $_SERVER['REMOTE_ADDR'];
$browser = $_SERVER['HTTP_USER_AGENT'];
$sendObj = new \stdClass();
if (isset($_POST['name']) && $_POST['comment']) {
$myFile = "comments.json";
$arr_data = array(); // create empty array
try{
//Get form data
$formdata = array(
'name'=> $_POST['name'],
'comment'=> $_POST['comment'],
'date' =>$dateAndTime,
'ip' => $ipAddress,
'browser' => $browser
);
//Get data from existing json file
$jsondata = file_get_contents($myFile);
// converts json data into array
$arr_data = json_decode($jsondata, true);
// print_r($formdata);
// Push user data to array
// array_push($arr_data,$formdata);
$arr_data[] = $formdata;
//Convert updated array to JSON
$jsondata = json_encode($arr_data, JSON_PRETTY_PRINT);
//write json data into data.json file
if(file_put_contents($myFile, $jsondata)) {
$sendObj->massage = "Done";
$sendObj->user = $_POST['name'];
$sendObj->comment = $_POST['comment'];
$sendObj->date = $dateAndTime;
}
else{
// echo "error";
$sendObj->massage = "Error";
$sendObj->cause = "Unable to save data on server";
}
}
catch (Exception $e) {
$sendObj->massage = "Error";
$sendObj->cause = "Exception occured " . $e->getMessage();
// echo 'Caught exception: ', $e->getMessage(), "\n";
}
}else{
$sendObj->massage = "Error";
$sendObj->cause = "no necessary data";
}
die(json_encode($sendObj));
?>