-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_blog.php
89 lines (73 loc) · 3.7 KB
/
new_blog.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" />
<link rel="stylesheet" href="../Blog/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="../Blog/css/style.css?<?php rand(1, 9) ?>" />
<title>New Blog</title>
</head>
<body>
<?php
include("./php/header.php");
include("./php/connect_DB.php");
// session_start();
if (!$_SESSION['login']) {
header('Location: login.php');
}
?>
<br><br>
<div class="container d-flex justify-content-center flex-column mt-5 w-100" id="form-container">
<div class="h1 mt-2 text-center">Start blogging today</div>
<div class="h6 text-center mb-4">Share your ideas with the world!</div>
<form class="mt-5" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="add_blog" method="POST">
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Your Name :</label>
<input type="text" class="form-control" id="exampleFormControlInput1" name="blog_writer" required>
</div>
<div class="mb-3">
<label for="exampleFormControlInput2" class="form-label">Blog Title :</label>
<input type="text" class="form-control" id="exampleFormControlInput2" name="blog_title" required>
</div>
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">Blog Body :</label>
<textarea class="form-control" id="exampleFormControlTextarea1" maxlength="1000" rows="3" name="blog_body" required></textarea>
</div>
<input type="submit" value="Submit" class="btn btn-outline-success">
</form>
</div>
<?php include("./php/footer.php"); ?>
<script src="../Blog/js/bootstrap.min.js"></script>
<script src="../Blog/js/jquery.min.js"></script>
<script src="../Blog/js/script.js"></script>
</body>
</html>
<?php
// date and time format
function blog_DOC() {
$currentDateTime = date("Y-m-d H:i:s");
$formattedDateTime = "Posted on " . date("Y-m-d") . " at " . date("h:i A", strtotime($currentDateTime));
return $formattedDateTime;
}
// insert a new blog
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$blog_writer = $_POST['blog_writer'];
$blog_title = $_POST['blog_title'];
$blog_body = $_POST['blog_body'];
if (!empty($blog_body) && !empty($blog_title) && !empty($blog_writer)) {
// Prepare the query with form data
$query = "INSERT INTO blogs (blog_Writer, blog_Title, blog_Body, blog_DOC, user_id) VALUES ('$blog_writer', '$blog_title', '$blog_body', '" . blog_DOC() . "', '" . $_COOKIE['login_user_id'] . "');";
// Execute the query
$result = mysqli_query($conn, $query);
if ($result) {
echo "<script> $('#form-container').before(`<div class='mt-2 alert alert-success alert-dismissible fade show text-center' role='alert'><strong>Blog successfully added.</strong><button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button></div>`)</script>";
} else {
echo "<script> $('#form-container').before(`<div class='mt-2 alert alert-danger alert-dismissible fade show text-center' role='alert'><strong>Error adding blog.</strong><button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button></div>`)</script>";
}
} else {
echo "<script> $('#form-container').before(`<div class='mt-2 alert alert-danger alert-dismissible fade show text-center' role='alert'><strong>Please Fill All Inputs.</strong><button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button></div>`)</script>";
}
}
?>