Skip to content

Commit

Permalink
submit form without refreshing page POG
Browse files Browse the repository at this point in the history
  • Loading branch information
BreadGuy007 committed Feb 19, 2022
1 parent f9724fe commit 8331d56
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 53 deletions.
66 changes: 25 additions & 41 deletions admin_manage_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<title>OG Tech - Manage Users Panel</title>
<?php
include "header.php";
Expand Down Expand Up @@ -43,15 +44,15 @@
<!-- search member input field end -->

<!-- search member result list start -->
<form action="admin_manage_users.php" method="GET">
<form id="refresh" action="" method="GET">
<table class="responsive-table">
<thead class="text-primary">
<tr><th>Username</th></tr>
</thead>
<tbody>
<?php
$oper = new adminContr;
$oper->usersList();
<?php
$Table = new adminContr;
$Table->usersList();
?>
</tbody>
</table>
Expand Down Expand Up @@ -87,7 +88,7 @@
<div class="card rounded-card">
<div class="card-content">
<span class="card-title orange-text bold">Create User</span>
<form id="create">
<form id="create" name="create" action="" method="post">
<div class="row">
<div class="input-field col s8 white-text">
<i class="material-icons prefix">account_circle</i>
Expand All @@ -114,7 +115,7 @@
<div class="row">
<div class="input-field col s8 white-text">
<i class="material-icons prefix white-text">assignment_ind</i>
<select name="level">
<select id="level" name="level">
<option value="" disabled selected>Choose your option</option>
<option value=1>Member</option>
<option value=2>Admin</option>
Expand All @@ -129,55 +130,38 @@
<label for="email" class="white-text">Email</label>
<span class="helper-text white-text" data-error="wrong" data-success="correct"></span>
<div id="message" class="errormsg">
<?php
if (isset($_GET["error"]))
{
if ($_GET["error"] == "empty_input")
echo "<p>*Fill in all fields!<p>";

else if ($_GET["error"] == "passwords_dont_match")
echo "<p>*Passwords doesn't match!</p>";

else if ($_GET["error"] == "invalid_uid")
echo "<p>*Choose a proper username!</p>";

else if ($_GET["error"] == "username_taken")
echo "<p>*Username/Email already taken!</p>";

else if ($_GET["error"] == "none")
echo "<p class='green-text'>Added User.</p>";
}
?>

</div>
</div>
</div>
<input class="btn orange btn-block z-depth-5" type="submit" name="submit_user" id="submit_btn" value="Create User">
<input class="btn orange btn-block z-depth-5" type="submit" name="submit_user" id="submit_user" value="Create User">
</form>
</div>
</div>
</div>
</div>
</body>

<script>
$(document).ready(function(){
$('select').formSelect();

$("#submit_btn").click(function(){
var formData = new FormData($(form)[0]);
$.ajax({
url: "includes/admin.inc.php",
method: "POST",
data: formData,
processData: false,
contentType: false,

success: function(data) {
$('form').trigger("reset");
}
$("#create").submit(function(e) {
event.preventDefault();
var username = $("#username").val();
var pwd = $("#pwd").val();
var repeat_pwd = $("#repeat_pwd").val();
var level = $("#level").val();
var email = $("#email").val();
var submit = $("#submit_user").val();
$("#message").load("includes/admin.inc.php", {
username: username,
pwd: pwd,
repeat_pwd: repeat_pwd,
level: level,
email: email,
submit: submit
});
return false;
});
})
});
</script>

Expand Down
4 changes: 4 additions & 0 deletions classes/commonUtil.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function setUser($username, $pwd, $email, $privilegeLevel=0) {
// create cart
$sql = "INSERT INTO Orders(MemberID) VALUES ($memberID);";
$result = $this->conn()->query($sql) or die("<p>*Cart creation error, please try again!</p>");
$this->conn()->close();
}

public function emptyInput($username, $pwd, $repeatPwd, $email)
Expand All @@ -50,4 +51,7 @@ public function invalidUid($username)

public function pwdNotMatch($pwd, $repeatPwd)
{ return $pwd !== $repeatPwd; }

public function EmptyInputCreateUser($username, $pwd, $repeatPwd, $privilegeLevel, $email)
{ return empty($username) || (empty($pwd)) || (empty($repeatPwd)) or ($privilegeLevel === "") || (empty($email));}
}
3 changes: 1 addition & 2 deletions header.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="./static/materialize/js/materialize.min.js"></script>
<link rel="stylesheet" href="./static/css/base.css">
</head>
Expand Down
23 changes: 14 additions & 9 deletions includes/admin.inc.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

if (isset($_POST["username"]))
require_once "class_autoloader.php";
if (isset($_POST["submit"]))
{
$username = $_POST["username"];
$pass = $_POST["pwd"];
Expand All @@ -10,29 +10,34 @@

$util = new CommonUtil;

if (EmptyInputCreateUser($username, $pass, $repeatPass, $email, $privilegeLevel))
if ($util->EmptyInputCreateUser($username, $pass, $repeatPass, $email, $privilegeLevel))
{
echo("<script>location.href = 'admin_manage_users.php?error=empty_input';</script>");
echo "<script>document.getElementById('message').className = 'errormsg';</script>";
echo "<script>document.getElementById('message').innerHTML = '*Fill in all fields!';</script>";
exit();
}
if ($util->pwdNotMatch($pass, $repeatPass))
{
echo("<script>location.href = 'admin_manage_users.php?error=passwords_dont_match';</script>");
echo "<script>document.getElementById('message').className = 'errormsg';</script>";
echo "<script>document.getElementById('message').innerHTML = '*Passwords doesn't match!';</script>";
exit();
}
if ($util->invalidUid($username))
{
echo("<script>location.href = 'admin_manage_users.php?error=invalid_uid';</script>");
echo "<script>document.getElementById('message').className = 'errormsg';</script>";
echo "<script>document.getElementById('message').innerHTML = '*Choose a proper username!';</script>";
exit();
}
if ($util->uidExists($username, $email))
{
echo("<script>location.href = 'admin_manage_users.php?error=username_taken';</script>");
echo "<script>document.getElementById('message').className = 'errormsg';</script>";
echo "<script>document.getElementById('message').innerHTML = '*Username/Email already taken!';</script>";
exit();
}

$privilegeLevel -= 1;
$util->setUser($username, $pass, $email, $privilegeLevel);
echo("<script>location.href = 'admin_manage_users.php?error=none';</script>");
$this->conn()->close();
echo "<script>document.forms['create'].reset()</script>";
echo "<script>document.getElementById('message').className = 'green-text';</script>";
echo "<script>document.getElementById('message').innerHTML = 'Added User.';</script>";
}
2 changes: 1 addition & 1 deletion manage_profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
if (isset($_GET["error"]))
{
if ($_GET["error"] == "empty_input")
echo "<script>document.getElementById('msg').innerHTML = 'Fill in all fields!';</script>";
echo "<script>document.getElementById('msg').innerHTML = '*Fill in all fields!';</script>";

else if ($_GET["error"] == "invalid_uid")
echo "<script>document.getElementById('msg').innerHTML = '*Choose a proper username!';</script>";
Expand Down

0 comments on commit 8331d56

Please sign in to comment.