-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubmit_contact.php
More file actions
77 lines (65 loc) · 2.58 KB
/
submit_contact.php
File metadata and controls
77 lines (65 loc) · 2.58 KB
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
<?php
session_start();
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Check if user is logged in
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
echo json_encode(['success' => false, 'message' => 'Please login to submit a comment.']);
exit;
}
$comment = trim($_POST['comment'] ?? '');
// Validate comment length
if (empty($comment)) {
echo json_encode(['success' => false, 'message' => 'Comment is required.']);
exit;
}
if (strlen($comment) < 10) {
echo json_encode(['success' => false, 'message' => 'Comment must be at least 10 characters.']);
exit;
}
// Get user info from session
$phone = $_SESSION['phone'] ?? '';
$firstName = $_SESSION['firstName'] ?? '';
$lastName = $_SESSION['lastName'] ?? '';
$dob = $_SESSION['dob'] ?? '';
$email = $_SESSION['email'] ?? '';
$gender = $_SESSION['gender'] ?? '';
// Generate unique contact-id
$contactId = 'CONTACT-' . time() . '-' . rand(1000, 9999);
// XML file path
$xmlFile = 'contact_submissions.xml';
// Load existing XML or create new
if (file_exists($xmlFile)) {
$xml = simplexml_load_file($xmlFile);
} else {
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><contacts></contacts>');
}
// Add new contact submission
$contact = $xml->addChild('contact');
$contact->addChild('contact-id', htmlspecialchars($contactId));
$contact->addChild('phone', htmlspecialchars($phone));
$contact->addChild('firstName', htmlspecialchars($firstName));
$contact->addChild('lastName', htmlspecialchars($lastName));
$contact->addChild('dob', htmlspecialchars($dob));
$contact->addChild('email', htmlspecialchars($email));
$contact->addChild('gender', htmlspecialchars($gender));
$contact->addChild('comment', htmlspecialchars($comment));
$contact->addChild('submission-date', date('Y-m-d H:i:s'));
// Save XML file
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
if ($dom->save($xmlFile)) {
echo json_encode([
'success' => true,
'message' => 'Comment submitted successfully!',
'contactId' => $contactId
]);
} else {
echo json_encode(['success' => false, 'message' => 'Failed to save submission.']);
}
} else {
echo json_encode(['success' => false, 'message' => 'Invalid request method.']);
}
?>