-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.php
64 lines (47 loc) · 1.34 KB
/
create.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
<?php
$app = require "./core/app.php";
// Create new instance of user
$user = new User($app->db);
$response = [
"ok" => false,
"row" => '',
'message' => 'There was an error submiting your request!',
];
// Validate input
$name = @$_POST['name'];
$email = @$_POST['email'];
$city = @$_POST['city'];
$phone = @$_POST['phone'] ?? '';
do{
if(!strlen($name))
$response['message'] = 'Used name can not be empty!';
break;
if(!strlen($city))
$response['message'] = 'City can not be empty!';
break;
if(!strlen($email))
$response['message'] = 'Email address seems to be invalid!';
break;
if($user->emailExists($email))
$response['message'] = 'Email address already exists!';
break;
}while(false);
// Build database data
$db_data = [
'name' => $name,
'email' => $email,
'city' => $city,
'phone' => $phone
];
// Insert it to database with POST data
$user->insert($db_data);
//Add data to response
$response['row'] .= '<td>'.htmlspecialchars($name).'</td>';
$response['row'] .= '<td>'.htmlspecialchars($email).'</td>';
$response['row'] .= '<td>'.htmlspecialchars($city).'</td>';
$response['row'] .= '<td>'.htmlspecialchars($phone).'</td>';
$response['message'] = 'Used added successfully!';
$response['ok'] = true;
// Write response
header('Content-Type: application/json');
echo json_encode($response);