PHP Script & Validation:
JavaScript
x
52
52
1
function SampleInput($data) {
2
$data = trim($data);
3
$data = stripslashes($data);
4
$data = htmlspecialchars($data);
5
return $data;
6
}
7
8
if (isset($_POST['RegisterClient'])) {
9
10
$file = $_FILES['avatar']['name'];
11
$file_location = $_FILES['avatar']['tmp_name'];
12
$folder = "../Public/img/uploads/profile-picture/";
13
$new_file_name = strtolower($file);
14
$final_file = str_replace(' ', '-', $new_file_name);
15
16
$name = htmlspecialchars(trim($_POST['name'], ENT_QUOTES));
17
$username = htmlspecialchars(trim($_POST['username'], ENT_QUOTES));
18
$email = htmlspecialchars(trim($_POST['email'], ENT_QUOTES));
19
$password = htmlspecialchars(trim($_POST['password'], ENT_QUOTES));
20
$cpassword = htmlspecialchars(trim($_POST['cpassword'], ENT_QUOTES));
21
$role = htmlspecialchars(trim($_POST['role'], ENT_QUOTES));
22
23
if (move_uploaded_file($file_location, $folder.$final_file)) {
24
$image = $final_file;
25
}
26
27
if(empty($_POST['name']) || empty($_POST['username']) || empty($_POST['password']) || empty($_POST['cpassword']) || empty($_POST['role'])) {
28
$error = "All fields are required!";
29
} else {
30
$name = SampleInput($_POST['name']);
31
$username = SampleInput($_POST['username']);
32
$password = SampleInput($_POST['password']);
33
$cpassword =SampleInput($_POST['cpassword']);
34
$role = SampleInput($_POST['role']);
35
if (!preg_match('/^[a-zA-Z0-9s]+$/',$name && $username && $password && $cpassword && $role)) {
36
$error = "Only letters and white space allowed";
37
}
38
}
39
40
$stmt = $db->prepare("INSERT INTO clients(name, username, email, password, avatar, role, registration_date) VALUES (:name, :username, :email, :password, :avatar, :role, NOW())");
41
$stmt->execute(array(
42
'name' => $name,
43
'username' => $username,
44
'email' => $email,
45
'password' => $password,
46
'avatar' => $image,
47
'role' => $role
48
));
49
50
$success = 'Registered!';
51
}
52
Modal:
JavaScript
1
65
65
1
<div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
2
<div class="modal-dialog" role="document">
3
<form action="" method="POST" enctype="multipart/form-data">
4
<div class="modal-content">
5
<div class="modal-header">
6
<h5 class="modal-title" id="exampleModalLabel">Create user</h5>
7
<button class="close" type="button" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
8
</button>
9
</div>
10
<div class="modal-body">
11
<?php
12
if ($error) {
13
echo '<div class="mb-3">' . $error . '</div>';
14
} else if ($success) {
15
echo '<div class="mb-3">' . $success . '</div>';
16
}
17
?>
18
<div class="row gx-3 mb-3">
19
<div class="col-md-12">
20
<label class="small mb-1" for="login-fullname">Full name*</label>
21
<input class="form-control" name="name" type="text" placeholder="Valid full name" />
22
</div>
23
</div>
24
<div class="mb-3">
25
<label class="small mb-1" for="login-username">Username (how your name will appear to other users on the site)</label>
26
<input class="form-control" name="username" type="text" placeholder="Valid username" />
27
</div>
28
<div class="mb-3">
29
<label class="small mb-1" for="login-email">Email address</label>
30
<input class="form-control" name="email" type="text" placeholder="Valid email" />
31
</div>
32
<div class="row gx-3 mb-3">
33
<div class="col-md-6">
34
<label class="small mb-1" for="login-password">Password</label>
35
<input class="form-control" name="password" type="password" placeholder="Strong password" />
36
</div>
37
<div class="col-md-6">
38
<label class="small mb-1" for="login-cpassword">Confirm Password</label>
39
<input class="form-control" name="cpassword" type="password" placeholder="Strong password" />
40
</div>
41
</div>
42
<div class="row gx-3 mb-3">
43
<div class="card-body text-center">
44
<input type="file" required class="btn btn-primary" name="avatar">
45
<div class="small font-italic text-muted mb-6">JPG or PNG no larger than 5 MB</div>
46
</div>
47
</div>
48
<div class="mb-3">
49
<label class="small mb-1" for="selectRole">Select user role</label>
50
<select class="form-control" name="role">
51
<option value="">Select user role</option>
52
<option value="admin">Admin</option>
53
<option value="user">User</option>
54
</select>
55
</div>
56
</div>
57
<div class="modal-footer">
58
<button class="btn btn-secondary" type="button" data-dismiss="modal">Close</button>
59
<button type="submit" name="RegisterClient" class="btn btn-primary">Create</button>
60
</div>
61
</div>
62
</form>
63
</div>
64
</div>
65
I don’t know where the problem is, I want the modal to remain open even if the problem or form has been submitted successfully. Now, with this code, if there is a problem with the submit form, the modal shuts down and if we open it again, the user can see error messages. Where am I wrong?
Advertisement
Answer
After a whole day, I resolve an issue with this.
JavaScript
1
5
1
$('#myModal').modal({
2
backdrop: 'static',
3
keyboard: false
4
})
5
With this code written above, a modal will be closed only if the user clicks on the close button. With this code, you disabled the close modal by pressing the ESC button on the keyboard.