-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateCouchDBdocument.php
99 lines (83 loc) · 3 KB
/
createCouchDBdocument.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php include_once 'CouchDBusernamepassword.php';
// declare variables to get the value from input
$nricError = $nameError = $mobileNumError = $selectDatabaseError = "";
// set a boolean variable to check if the fields have errors and retrun true if no error was detected
$valid = True;
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//===================== nric validation ==========================
// if the nric name field is empty
if (empty($_POST["nric"])){
$nricError = "Please enter your nric.";
$_POST["nric"] = "";
$valid = False;
}
// else if the nric name field contains numbers
else if (strlen($_POST["nric"]) != 9){
$nricError = "Please enter according to the format.";
$_POST["nric"] = "";
$valid = False;
}
//===================== name validation ==========================
// if the name name field is empty
if (empty($_POST["name"])){
$nameError = "Please enter your name.";
$_POST["name"] = "";
$valid = False;
}
// else if the name name field contains numbers
else if (!ctype_alpha($_POST["name"])){
$nameError = "Please enter letters only.";
$_POST["name"] = "";
$valid = False;
}
//===================== nmobile number validation ==========================
// if the mobile number name field is empty
if (empty($_POST["mobileNum"])){
$mobileNumError = "Please enter your mobile number";
$_POST["mobileNum"] = "";
$valid = False;
}
// else if the mobile number name field contains aplhabets
else if (ctype_alpha($_POST["mobileNum"])){
$nameError = "Please enter numbers only.";
$_POST["mobileNum"] = "";
$valid = False;
}
// else if the mobile number name field contains numbers less than 8
else if (strlen($_POST["mobileNum"]) != 8){
$mobileNumError = "Please enter 8 digits.";
$_POST["mobileNum"] = "";
$valid = False;
}
//===================== database selected validation ==========================
// if no database is selected
// if the mobile number name field is empty
if (empty($_POST["databaseSelected"])){
$selectDatabaseError = "You have not selected any database.";
$valid = False;
}
// if there are no errors in the create database form, it will proceed to insert the database
if($valid = True) {
$ch = curl_init();
$personDetails = array(
'nric' => $_POST["nric"],
'name' => $_POST["name"],
'mobileNum' => $_POST["mobileNum"]
);
$person = json_encode($personDetails);
curl_setopt($ch, CURLOPT_URL, 'http://localhost:5984/'.$_POST["databaseSelected"]. '/' .$personDetails['nric']);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $person);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Accept: /',
));
curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
$response = curl_exec($ch);
echo $response;
curl_close($ch);
header("Location: documents.php?message=createDocumentSuccess&database=".$_POST["databaseSelected"]);
}
}
?>