users can now add their cupboards

This commit is contained in:
Louis Vallat 2019-11-10 15:44:20 +01:00
parent 7fb7758df2
commit 0283fb1bc5

91
add-cupboard.php Normal file
View File

@ -0,0 +1,91 @@
<?php
require_once("./assets/php/utils.php");
// Check if the user is logged in, if not then redirect him to login page
if (!is_connected()) {
header("location: login.php");
exit;
}
// ========================================================================
// IF IN POST : USER SUBMITTED THE FORM
// ========================================================================
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Set all variables to empty.
$cupboard_name = $cupboard_description = "";
$cupboard_name_err = $cupboard_description_err = "";
// ========================================================================
// USER INPUT VALIDATION AND SANITIZATION PROCESS
// ========================================================================
// Check and store every field the user entered.
// NAME
if (empty(trim($_POST["name"]))) {
$cupboard_name_err = "Please input a cupboard name.";
} else if (strlen(trim($_POST["name"])) > 255) {
$cupboard_name_err = "Max size for the cupboard name is 255 characters. "
. "Yours was " . strlen(trim($_POST["name"])) . " characters long.";
} else {
$cupboard_name = trim($_POST["name"]);
}
// DESCRIPTION
if (empty(trim($_POST["description"]))) {
$cupboard_description = null;
} else if (strlen(trim($_POST["description"])) > 65535) {
$cupboard_description_err = "Your description is too long.";
} else {
$cupboard_description = trim($_POST["description"]);
}
// ========================================================================
// ========================================================================
// INSERTION IN DATABASE IF CORRECT
// ========================================================================
if (empty($cupboard_name_err) && empty($cupboard_description_err)) {
if (!add_cupboard($cupboard_name, $cupboard_description)) {
echo "Oops! Something went wrong. Please try again later.";
}
} else {
echo $cupboard_name_err;
echo $cupboard_description_err;
}
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
<label>Nom</label>
<input type="text" name="name" class="form-control" value="" required>
<span class="help-block"></span>
</div>
<div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
<label>Description</label>
<input type="text" name="description" class="form-control" value="">
<span class="help-block"></span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Test">
</div>
</form>
</body>
</html>