-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit-request-complaint.php
83 lines (68 loc) · 2.51 KB
/
submit-request-complaint.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
<?php
session_start();
require_once("php/includes/dbFunctions.php");
require_once("php/includes/validateFunctions.php");
//array to store mandatoriness of each field of form
$required = array(
"complaint_type"=> True,
"description"=>False,
"request_id"=>True
);
$values = array(); //array to store values from form
$errors = array(); //array to store errors relevent to each field
foreach ($required as $fieldName=>$_) //initialize arrays with empty values
{
$values[$fieldName] = '';
$errors[$fieldName] = '';
}
$errors['form'] = ''; // variable to store form errors
if(isset($_POST['complaint_type'])) //check for a post method
{
//get values from user
foreach ($values as $fieldName=>$value)
{
$values[$fieldName] = $_POST[$fieldName];
}
$isValid = True; //variable to store if the form details are valid
if(areRequiredFieldsProvided($values, $required, $errors))
{
if(validateNumericValues($values, $errors, array('request_id')))
{
//validate complaint type
if(!($values['complaint_type'] == 'false advertisement' or $values['complaint_type'] == 'spam and abuse' or $values['complaint_type'] == 'false information' or $values['complaint_type'] == 'transaction denial'))
{
$isValid = False;
$errors['complaint_type'] = 'invalid reason';
}
//validate description
if(strlen($values['description']) > 500)
{
$isValid = False;
$errors['description'] = 'description should be less than 500 characters';
}
}
else
{
$isValid = False;
}
}
else
{
$isValid = False;
}
if ($isValid) //if valid add to database
{
$result = addRequestComplaint($values, $_SESSION['user_id']);
if(!$result)
{
$isValid = False;
$errors['form'] = 'failed to submit the complaint';
}
}
$toSend = array(); //array to send as result
if ($isValid) $toSend['success'] = True;
else $toSend['success'] = False;
$toSend['errors'] = $errors;
echo json_encode($toSend); //send the results in json format
}
?>