-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost_courses.php
More file actions
158 lines (147 loc) · 5.15 KB
/
post_courses.php
File metadata and controls
158 lines (147 loc) · 5.15 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
// Database connection
$servername = "localhost";
$username = "root"; // Update with your database username
$password = ""; // Update with your database password
$dbname = "coursehub"; // Replace with your database name
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Handle form submission
$feedback_message = ""; // Holds success or error message
$success = false; // Flag to indicate success
$form_submitted = false; // Flag to check if form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['course_name'], $_POST['course_link'])) {
$course_name = trim($_POST['course_name']);
$course_link = trim($_POST['course_link']);
// Validate inputs
if (!empty($course_name) && !empty($course_link)) {
// Check for duplicate course name
$check_query = $conn->prepare("SELECT COUNT(*) FROM courses WHERE course_name = ?");
$check_query->bind_param("s", $course_name);
$check_query->execute();
$check_query->bind_result($count);
$check_query->fetch();
$check_query->close();
if ($count > 0) {
$feedback_message = "Error: Course name already exists.";
} else {
// Insert the new course
$stmt = $conn->prepare("INSERT INTO courses (course_name, course_link) VALUES (?, ?)");
if ($stmt) {
$stmt->bind_param("ss", $course_name, $course_link);
if ($stmt->execute()) {
$feedback_message = "Course added successfully!";
$success = true; // Set success flag to true
$form_submitted = true; // Form is submitted successfully
} else {
$feedback_message = "Error: Could not add course. " . $stmt->error;
}
$stmt->close();
} else {
$feedback_message = "Error preparing statement: " . $conn->error;
}
}
} else {
$feedback_message = "All fields are required.";
}
}
// Close database connection
$conn->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Post Course</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f7fc;
color: #333;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.container {
background: #fff;
padding: 40px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
text-align: center;
max-width: 400px;
width: 100%;
}
h1 {
margin-bottom: 20px;
color: #2c3e50;
}
form {
margin-bottom: 20px;
}
input, button {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
button {
background-color: #3498db;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #2980b9;
}
.message {
margin-top: 10px;
font-size: 16px;
color:rgb(39, 82, 174);
}
.error {
color: #e74c3c;
}
.form-container {
display: <?php echo $form_submitted ? 'none' : 'block'; ?>;
}
</style>
</head>
<body>
<div class="container">
<h1>Post Course</h1>
<!-- Only show form if it's not submitted -->
<div class="form-container">
<form method="POST">
<input type="text" name="course_name" placeholder="Course Name" required>
<input type="url" name="course_link" placeholder="Course Link" required>
<button type="submit">Post Course</button>
</form>
</div>
<?php if (!empty($feedback_message)) : ?>
<!-- Show feedback message if any -->
<div class="message <?php echo (strpos($feedback_message, 'Error') !== false) ? 'error' : ''; ?>">
<?php echo htmlspecialchars($feedback_message); ?>
</div>
<?php endif; ?>
</div>
<script>
<?php if ($success): ?>
// Display success pop-up message and redirect to postcourses.html
alert('Course added successfully!');
window.location.href = "post_courses.html"; // Redirect after clicking OK
<?php elseif (!empty($feedback_message)): ?>
// Display error pop-up message
alert('<?php echo htmlspecialchars($feedback_message); ?>');
<?php endif; ?>
</script>
</body>
</html>