-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignup.php
211 lines (181 loc) · 5.29 KB
/
signup.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!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 href="libraries/bootstrap.min.css" rel="stylesheet">
<title>Form Validation</title>
<style>
body {
margin: 0;
padding: 0;
}
main {
display: flex;
justify-content: center;
align-items:center;
margin-top:80px;
padding: 20px 0px;
}
form {
background-color: gainsboro;
width: 600px;
padding: 30px 40px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
color: grey;
}
h1{
text-align: center;
color: #333;
}
.error{
color: red;
font-size: 12px;
}
label {
display: block;
margin-bottom: 1px;
font-weight: bold;
}
input[type="text"],
input[type="email"],
input[type="password"],
textarea {
width: 100%;
padding: 10px 8px;
border: none;
border-radius: 4px;
background-color: #f4f4f4;
color: #333;
}
input[type="radio"] {
margin-right: 10px;
}
textarea {
resize: vertical;
margin-bottom: 8px;
}
input[type="submit"] {
width: 100%;
padding: 8px;
margin-block:20px;
border: none;
border-radius: 4px;
color: #fff;
font-size: 16px;
cursor: pointer;
}
a{
text-decoration: none;
margin-bottom: 10%;
}
</style>
</head>
<body>
<?php
require "./dbConnection.php";
$nameError = $emailError = $passwordError = $genderError = "";
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$name = $email = $password = $gender = $message = "";
//String/Name Validation
if(empty($_POST['name'])){
$nameError = "Name is required *";
}
else{
$name = $_POST['name'];
if(!preg_match("/^[a-zA-Z\s]*$/", $name)){
$nameError = "Only alphabets and white space are allowed";
}
}
//Email Validation
if(empty($_POST['email'])){
$emailError = "Email is required *";
}
else{
$email = $_POST['email'];
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$emailError = "Invalid Email *";
}
}
//password validation
if(empty($_POST['password'])){
$passwordError = " password is required *";
}
else{
$password =password_hash($_POST['password'],PASSWORD_DEFAULT);
}
//gender validation
if(empty($_POST['gender'])){
$genderError = " Gender is required *";
}
else{
$gender = $_POST['gender'];
}
//message validation
if(empty($_POST['message'])){
$message = "";
}
else{
$message = $_POST['message'];
}
if(isset($_POST['submit'])){
if($nameError == "" && $emailError == "" && $passwordError == "" &&$genderError == ""){
//check if email already exists
$emailCheckQuery = "SELECT * FROM users WHERE email = '$email' LIMIT 1";
$result = mysqli_query($connection, $emailCheckQuery);
if(mysqli_num_rows($result)>0){
$emailError = "Email already exists !";
echo "Email already exists!";
}
else{
$sql ="INSERT INTO Users (name, email, password, gender, message) VALUES(
'$name', '$email', '$password', '$gender', '$message'
)";
if(mysqli_query($connection, $sql)){
// echo " Data is inserted successfully!";
header("Location: login.php");
//clear input fields after form submission
$name = $email = $password = $gender = $message = "";
}
else{
echo " Failed data insertion!" . mysqli_error($connection);
}
}
}
}
}
?>
<main>
<form action="signup.php" method="post" >
<h1>Sign Up</h1>
<label>Full Name:</label><br>
<span class="error"><?php echo $nameError?></span>
<input type="text" name="name" class="form-control" placeholder="Enter your full Name" autocomplete="off">
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Email address</label><br>
<span class="error"><?php echo $emailError?></span>
<input type="email" name="email" class="form-control" id="exampleFormControlInput1" placeholder="[email protected]">
</div>
<label>Password:</label><br>
<span class="error"><?php echo $passwordError?></span>
<input type="password" name="password" placeholder="Enter your password" autocomplete="off"><br>
<label>Gender:</label>
<span class="error"><?php echo $genderError?></span><br>
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="other">Other
<br><br>
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">Message</label>
<textarea class="form-control" name="message" id="exampleFormControlTextarea1" rows="3"></textarea>
</div>
<!-- <label>Message</label><br>
<textarea name="message" id="" cols="30" rows="8" placeholder="Enter your message here..."></textarea> -->
<input type="submit" value="Submit" name="submit" class="btn btn-primary">
<a href="./login.php">Already Registered?login here</a>
</form>
</main>
<script src="libraries/bootstrap.min.js"></script>
</body>
</html>