On the website, there is a form where admins can create accounts these accounts can be user or admin, currently on the website when I fill in the form fields and post the form data to the form processing file, something happens, I've been looking around but I can't seem to find any solution to this.
<?php
session_start();
if (!isset($_SESSION['accessRights']) || $_SESSION['accessRights'] == false) {
header("location:../../index.php");
}
if (!isset($_SESSION['accessRights']) || $_SESSION['accessRights'] == "user") {
header("location:../../index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="../css/regform.css">
<link rel="stylesheet" type="text/css" href="../css/bookstore.css">
<script src="../js/formValidation.js" defer></script>
<title>Bookstore</title>
</head>
<body>
<!--nav bar-->
<header>
<div class="logo"><img src="../images/logo.png" alt="book logo"></div>
<nav>
<ul>
<li id="chanlog">Changelog</li>
<li>View Book</li>
<li>Add Book</li>
<li class="active">Create Account</li>
<li>Logout</li>
</ul>
</nav>
<div class="menu-toggle"></div>
</header>
<div class="reg-title">
<h1>Create Account</h1>
</div>
<!--reg form-->
<div id="form-container" class="sub-heading">
<h2>Login Details</h2>
<form id="regform" action="../../controller/processRegistration.php" method="POST">
<div class="form-col">
<div class="field-icon">
<label for="uname">Username<span class="required"></span></label>
<i class="fas fa-user"></i>
<input type="text" name="user" placeholder="Username" required>
<div class="error hide">Username is required</div>
</div>
<div class="field-icon">
<label for="upass">Password<span class="required"></span></label>
<i class="fas fa-lock"></i>
<input type="text" name="pass" placeholder="Password" required>
<div class="error hide">Password must contain at least 8 characters</div>
</div>
<div class="field-icon">
<label for="urole">Access Rights<span class="required"></span></label>
<i class="fas fa-key"></i>
<input type="text" name="access" placeholder="Access Rights" required>
<div class="error hide">Access rights is required</div>
</div>
</div>
<!--extra details-->
<h2>User Details</h2>
<div class="form-col-2">
<div class="field-icon">
<label for="fname">First Name<span class="required"></span></label>
<i class="fas fa-user"></i>
<input type="text" name="fname" placeholder="First Name" required>
<div class="error hide">Firstname is required</div>
</div>
<div class="field-icon">
<label for="lname">Last Name<span class="required"></span></label>
<i class="fas fa-user"></i>
<input type="text" name="lname" placeholder="Last Name" required>
<div class="error hide">Lastname is required</div>
</div>
<div class="field-icon">
<label for="email">Email<span class="required"></span></label>
<i class="fas fa-at"></i>
<input type="text" name="email" placeholder="Email" required>
<div class="error hide">Email must contain # symbol</div>
</div>
</div>
<div class="btn-submit">
<input type="submit" id="send-form" value="Sign Up">
</div>
</form>
</div>
<footer><p>2020 Bookstore. All rights reserved.</p></footer>
</body>
</html>
\Below is where form data in the registration form is sent
<?php
session_start();
require("../model/dbConnection.php");
require("../model/dbFunctions.php");
require("inputFilter.php");
if (!empty([$_POST])) {
$Uname = inputFilter($_POST['user']);
$mypass = inputFilter($_POST['pass']);
$accRights = inputFilter($_POST['access']);
$Fname = inputFilter($_POST['fname']);
$Lname = inputFilter($_POST['lname'])
$Email = inputFilter($_POST['email']);
$LogID = inputFilter($_POST['loginid']);
// password hashed with PASSWORD_HASH()
$hPass = password_hash($mypass, PASSWORD_DEFAULT);
$query = $conn->prepare("SELECT username FROM login WHERE username = :user");
$query->bindValue(":user", $Uname);
$query->execute();
if ($query->rowCount() < 1) { //check if the user exists
newUser($Uname, $hPass, $accRights, $Fname, $Lname, $Email);
echo "User account has been created";
// redirect them to homepage
header('location:../view/pages/viewBook.php');
}
else {
echo "User already exists";
}
}
?>
\Below is the function to insert into database
function newUser($Uname, $hPass, $accRights, $Fname, $Lname, $Email)
{
global $conn;
try {
$conn->beginTransaction();
$stmt = $conn->prepare("INSERT INTO login(username, password, accessRights)
VALUES (:user, :pass, :access)");
$stmt->bindValue(':user', $Uname);
$stmt->bindValue(':pass', $hPass);
$stmt->bindValue(':access', $accRights);
$stmt->execute();
$lastUserID = $conn->lastInsertId();
$stmt = $conn->prepare("INSERT INTO users(firstName, lastName, email, loginID)
VALUES (:fname, :lname, :email, :loginID)");
$stmt->bindValue(':fname', $Fname);
$stmt->bindValue(':lname', $Lname);
$stmt->bindValue(':email', $Email);
$stmt->bindValue(':loginID', $lastUserID);
$stmt->execute();
$conn->commit(); // save to the db
}
catch (PDOException $ex) {
$conn->rollBack(); // if problem rollback
throw $ex;
}
}
Related
I have two files
functions.php
<?php
include 'config.php';
function signup(){
if (isset($_POST['submit'])) {
$uname = $_POST['uname'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
if($password == $cpassword) {
$hash = md5($password);
$insert = "INSERT INTO `users`(`user_name`, `email`, `password`) VALUES ('$uname','$email','$hash')";
$result = mysqli_query($con, $insert);
if ($result) {
echo '<script>alert("Your account has been successfully created.")</script>';
}
}
else {
echo '<script>alert("Passwords do not match!")</script>';
}
}
}
?>
signup.php
<?php
include 'functions.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- ===== Iconscout CSS ===== -->
<link rel="stylesheet" href="https://unicons.iconscout.com/release/v4.0.0/css/line.css">
<!-- ===== CSS ===== -->
<link rel="stylesheet" href="css/credential.css">
<title>Sing Up</title>
</head>
<body>
<div class="container">
<div class="forms">
<div class="form signup">
<span class="title">Sign Up</span>
<form method="POST" action="functions.php">
<div class="input-field">
<input type="text" name="uname" placeholder="Enter your full name" required>
<i class="uil uil-user"></i>
</div>
<div class="input-field">
<input type="email" name="email" placeholder="Enter your email" required>
<i class="uil uil-envelope icon"></i>
</div>
<div class="input-field">
<input type="password" class="password" name="password" placeholder="Create a password" required>
<i class="uil uil-lock icon"></i>
</div>
<div class="input-field">
<input type="password" class="password" name="cpassword" placeholder="Confirm a password" required>
<i class="uil uil-lock icon"></i>
<i class="uil uil-eye-slash showHidePw"></i>
</div>
<div class="checkbox-text">
<div class="checkbox-content">
<input type="checkbox" id="termCon">
<label for="termCon" class="text">I accepted all Terms and Conditions, Privacy Policy and Cookie Policy</label>
</div>
</div>
<div class="input-field button">
<input type="submit" value="Sign Up" name="submit">
</div>
</form>
<div class="login-signup">
<span class="text">Already have an account?
Login Now
</span>
</div>
</div>
<div class="form login">
<span class="title">Login</span>
<form action="#">
<div class="input-field">
<input type="email" placeholder="Enter your email" required>
<i class="uil uil-envelope icon"></i>
</div>
<div class="input-field">
<input type="password" class="password" placeholder="Enter your password" required>
<i class="uil uil-lock icon"></i>
<i class="uil uil-eye-slash showHidePw"></i>
</div>
<div class="checkbox-text">
<div class="checkbox-content">
<input type="checkbox" id="logCheck">
<label for="logCheck" class="text">Remember me</label>
</div>
Forgot password?
</div>
<div class="input-field button">
<input type="submit" value="Login" name="submit">
</div>
</form>
<div class="login-signup">
<span class="text">Don't have an account?
Signup Now
</span>
</div>
</div>
</div>
</div>
<script src="js/credential.js"></script>
</body>
</html>
I want something like this...
when I click on <input type="submit" of signup the signup() function from functions.php should work. But I don't know how to do it.
If I remove function signup(){} from functions.php and try without function then in url signup.php is replaced by functions.php and page is blank and no data is inserted in mysql localhost.
In 'config.php' file
<?php
$con = mysqli_connect("localhost","root","","get-viewed");
?>
Database name, Table name and field name are perfect I have double checked it.
The form action correctly point to function.php and the webserver execute it.
The result is blank because nothing in function.php get executed.
you defined function signup() but you don't call it
add signup(); as last code line, just before php closing tag ?>
Note 1: you can extract the code from the signup function, since it does not add any advantage.
Note 2: if the php closing tag is the last code line in the file (no html follow) you should omit, it is a good practice to avoid unwanted output.
This is a must once you start to use frameworks, otherwise header errors will popup
Thanks for helping me I have solved my question.
I updated functions.php
<?php
include 'config.php';
function signup() {
$uname = $_POST['uname'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
if($password == $cpassword) {
$hash = password_hash($password, PASSWORD_DEFAULT);
$insert = "INSERT INTO `users`(`user_name`, `email`, `password`) VALUES ('$uname','$email','$hash')";
$result = mysqli_query($con, $insert);
if ($result) {
echo '<script>alert("Your account has been successfully created.")</script>';
}
}
else {
echo '<script>alert("Passwords do not match!");location.replace("signup.php");</script>';
}
}
function login(){
if (isset($_POST['login'])) {
echo '<script>alert("login")</script>';
}
}
if (isset($_POST['signup'])) {
signup();
}
else {
login();
}
Now it is working perfectly as I wanted.
This question already has an answer here:
Trying to access array offset on value of type bool
(1 answer)
Closed 1 year ago.
When I want to register a new user I have error
Notice: Trying to access array offset on value of type bool in P:\xampp\htdocs\BTP\v4\register.php on line 37
Notice: Trying to access array offset on value of type bool in P:\xampp\htdocs\BTP\v4\register.php on line 39
Still, it all works, and user create
but I want to fix it and I don't know what's wrong
<?php
require_once 'include/database.php';
if(isset($_POST['btn_register'])){
$fname = strip_tags($_REQUEST['fname']);
$lname = strip_tags($_REQUEST['lname']);
$email = strip_tags($_REQUEST['email']);
$phone = strip_tags($_REQUEST['phone']);
$password = strip_tags($_REQUEST['password']);
$rpassword = strip_tags($_REQUEST['passwordrepet']);
if(empty($fname)){
$errorMsg[] = "Proszę podać imię";
}else if(empty($lname)){
$errorMsg[] = "Proszę podać nazwisko";
}else if(empty($email)){
$errorMsg[] = "Proszę podać adres e-mail";
}else if(empty($password)){
$errorMsg[] = "Proszę podać hasło";
}else if(empty($rpassword)){
$errorMsg[] = "Należy ponownie podać hasło";
}else if(strlen($password) < 6){
$errorMsg[] = "Hasło musi zawierać conajmniej 6 znaków";
}else if($password != $rpassword){
$errorMsg[] = "Hasła nie są takie same";
}else{
try{
$select_stmt = $db->prepare("SELECT email, phone FROM user WHERE email=:email OR phone=:phone");
$select_stmt->bindValue(':email', $email, PDO::PARAM_STR);
$select_stmt->bindValue(':phone', $phone, PDO::PARAM_STR);
$select_stmt->execute();
$row = $select_stmt->fetch(PDO::FETCH_ASSOC);
if($row['email'] == $email){
$errorMsg[] = "Adres e-mail jest już zajęty";
}else if($row['phone'] == $phone){
$errorMsg[] = "Numer telefonu został juz podany do innego konta";
}else if(!isset($errorMsg)){
$new_password = password_hash($password, PASSWORD_DEFAULT);
$insert_stmt = $db->prepare("INSERT INTO user (fname, lname, email, phone, password) VALUES (:fname, :lname, :email, :phone, :password)");
if($insert_stmt->execute(array( ':fname' =>$fname,
':lname' =>$lname,
':email' =>$email,
':phone' =>$phone,
':password' => $new_password))){
$registerMsg = "Udało sie zarejestrować";
}
}
}catch(PDOException $e){
echo $e->getMessage();
}
}
}
?>
<!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, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Best Trip Partner Register</title>
<!-- Custom fonts for this template-->
<link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">
<!-- Custom styles for this template-->
<link href="css/main.css" rel="stylesheet">
</head>
<body class="bg-gradient-primary">
<div class="container">
<div class="card o-hidden border-0 shadow-lg my-5">
<div class="card-body p-0">
<!-- Nested Row within Card Body -->
<div class="row">
<div class="col-lg-5 d-none d-lg-block bg-register-image"></div>
<div class="col-lg-7">
<div class="p-5">
<div class="text-center">
<h1 class="h4 text-gray-900 mb-4">Utwórz konto</h1>
</div>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" class="user">
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<input type="text" name="fname" class="form-control form-control-user" id="exampleFirstName" placeholder="Imię">
</div>
<div class="col-sm-6">
<input type="text" name="lname" class="form-control form-control-user" id="exampleLastName" placeholder="Nazwisko">
</div>
</div>
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<input type="email" name="email" class="form-control form-control-user" id="exampleInputEmail" placeholder="Adres e-mail">
</div>
<div class="col-sm-6">
<input type="phone" name="phone" class="form-control form-control-user" id="exampleInputPhone" placeholder="Numer telefonu">
</div>
</div>
<div class="form-group row">
<div class="col-sm-6 mb-3 mb-sm-0">
<input type="password" name="password" class="form-control form-control-user" id="exampleInputPassword" placeholder="Hasło">
</div>
<div class="col-sm-6">
<input type="password" name="passwordrepet" class="form-control form-control-user" id="exampleRepeatPassword" placeholder="Powtórz hasło">
</div>
</div>
<?php
print_r($row);
if(isset($errorMsg)){
foreach($errorMsg as $error){
?>
<div class="alert alert-danger">
<strong><?php echo $error; ?></strong>
</div>
<?php
}
}
if(isset($registerMsg)){
?>
<div class="alert alert-success">
<strong><?php echo $registerMsg; ?></strong>
</div>
<?php
}
?>
<button type="submit" name="btn_register" class="btn btn-primary btn-user btn-block">Zarejestruj się</button>
</form>
<hr>
<div class="text-center">
<a class="small" href="forgot-password.php">Zapomniałeś hasła?</a>
</div>
<div class="text-center">
<a class="small" href="login.php">Masz już konto? Zaloguj się!</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Bootstrap core JavaScript-->
<script src="vendor/jquery/jquery.min.js"></script>
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Core plugin JavaScript-->
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>
<!-- Custom scripts for all pages-->
<script src="js/sb-admin-2.min.js"></script>
</body>
</html>
Please use array_key_exists() function to check if the key email and phone exist in your variable.
Something like...
if (array_key_exists("email", $row)) {
if ($row['email'] == $email) {
$errorMsg[] = "Adres e-mail jest już zajęty";
}
}
I am currently using a PHP script to add a record into my database, this works properly after some debugging. When the script runs, it opens a new page with the query output.
I'd like to display the query output on the same page that executes the query, at the top of the page, under my navbar.
Below is my current page.
This is the desired result.
The relevant code of the HTML page:
<form name="registration" action="addKlant.php" target="_blank" method="post">
<h3><i class="fa fa-plus-circle"></i> Klant toevoegen</h3>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Naam</label>
<input type="text" name="klantNaam">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Soort klant</label>
<select class="" name="klantSoort">
<option value="">Bedrijf</option>
<option value="">Particulier</option>
</select>
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Telefoonnummer</label>
<input type="number" name="klantTel">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> E-mail adres</label>
<input type="email" name="klantEmail">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Straat & huisnr</label>
<input type="text" name="klantAdres">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Postcode</label>
<input type="text" name="klantPostcode">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Plaats</label>
<input type="text" name="klantPlaats">
</div>
<div class="form-input">
<input type="submit" name="submit" value="Klant toevoegen">
</div>
</form>
addKlant.php
<?php
require('db.php');
$stmt = $con->prepare('INSERT INTO `klanten` (klant_naam, klant_soort, klant_telefoon, klant_email, klant_straat, klant_postcode, klant_plaats) VALUES (?, ?, ?, ?, ?, ?, ?)');
if($stmt) {
$klant_naam = $_POST['klantNaam'];
$klant_soort = $_POST['klantSoort'];
$klant_telefoon = $_POST['klantTel'];
$klant_email = $_POST['klantEmail'];
$klant_adres = $_POST['klantAdres'];
$klant_postcode = $_POST['klantPostcode'];
$klant_plaats = $_POST['klantPlaats'];
$stmt->bind_param('ssissss', $klant_naam, $klant_soort, $klant_telefoon, $klant_email, $klant_adres, $klant_postcode, $klant_plaats);
$stmt->execute();
$con->close();
echo "Klant is toegevoegd, je kan deze pagina nu sluiten.";
}
?>
I was thinking about using something like if($_POST)? But I do not know how to implement it exactly.
It does not matter if the page refreshes to add the output, but I would like to keep it to PHP and not use JS (of course, if it is possible with only PHP).
Any tips are greatly appreciated.
EDIT: Edited my code with some help from #SloanTrasher
<?php
if($_POST['submit'] == 'Klant toevoegen') {
require('db.php');
$stmt = $con->prepare('INSERT INTO `klanten` (klant_naam, klant_soort, klant_telefoon, klant_email, klant_straat, klant_postcode, klant_plaats) VALUES (?, ?, ?, ?, ?, ?, ?)');
if($stmt) {
$klant_naam = $_POST['klantNaam'];
$klant_soort = $_POST['klantSoort'];
$klant_telefoon = $_POST['klantTel'];
$klant_email = $_POST['klantEmail'];
$klant_adres = $_POST['klantAdres'];
$klant_postcode = $_POST['klantPostcode'];
$klant_plaats = $_POST['klantPlaats'];
$stmt->bind_param('ssissss', $klant_naam, $klant_soort, $klant_telefoon, $klant_email, $klant_adres, $klant_postcode, $klant_plaats);
$stmt->execute();
$con->close();
}
}
?>
<!-- I placed it above the form for simplification -->
<?php ($_POST['submit'] == 'Klant toevoegen') {echo "<p>Klant is toegevoegd.</p>";}?>
<form name="registration" action="addKlant.php" target="_blank" method="post">
<h3><i class="fa fa-plus-circle"></i> Klant toevoegen</h3>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Naam</label>
<input type="text" name="klantNaam">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Soort klant</label>
<select class="" name="klantSoort">
<option value="">Bedrijf</option>
<option value="">Particulier</option>
</select>
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Telefoonnummer</label>
<input type="number" name="klantTel">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> E-mail adres</label>
<input type="email" name="klantEmail">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Straat & huisnr</label>
<input type="text" name="klantAdres">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Postcode</label>
<input type="text" name="klantPostcode">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Plaats</label>
<input type="text" name="klantPlaats">
</div>
<div class="form-input">
<input type="submit" name="submit" value="Klant toevoegen">
</div>
</form>
EDIT 2: Working code for anyone interested
<?php
include('auth.php');
if($_POST['submit'] == 'Klant toevoegen') {
require('db.php');
$stmt = $con->prepare('INSERT INTO `klanten` (klant_naam, klant_soort, klant_telefoon, klant_email, klant_straat, klant_postcode, klant_plaats) VALUES (?, ?, ?, ?, ?, ?, ?)');
if($stmt) {
$klant_naam = $_POST['klantNaam'];
$klant_soort = $_POST['klantSoort'];
$klant_telefoon = $_POST['klantTel'];
$klant_email = $_POST['klantEmail'];
$klant_adres = $_POST['klantAdres'];
$klant_postcode = $_POST['klantPostcode'];
$klant_plaats = $_POST['klantPlaats'];
$stmt->bind_param('ssissss', $klant_naam, $klant_soort, $klant_telefoon, $klant_email, $klant_adres, $klant_postcode, $klant_plaats);
$stmt->execute();
$con->close();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<!-- Charset, viewport en keywords -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Titel en favicon -->
<title></title>
<link rel="icon" href="./img/favicon.jpg">
<!-- Scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="./js/pageForward.js"></script>
<script type="text/javascript" src="./js/pageBack.js"></script>
<!-- Stylesheets en fonts -->
<link rel="stylesheet" href="./css/backend_styles.css">
<link href="https://fonts.googleapis.com/css?family=Work+Sans:200" rel="stylesheet"> <!-- font-family: 'Work Sans', sans-serif; -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300" rel="stylesheet"> <!-- font-family: 'Open Sans', sans-serif; -->
<link href="https://use.fontawesome.com/releases/v5.0.6/css/all.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="top-links">
<p><i class="fa fa-home"></i><i class="fa fa-cogs"></i><a onclick="goBack()"><i class="fa fa-arrow-left"></i></a><a onclick="goForward()"><i class="fa fa-arrow-right"></i></a> Backoffice | Ingelogt als: <?php echo $_SESSION['username']; ?> | Uitloggen</p>
</div>
<?php if($_POST['submit'] == 'Klant toevoegen') {echo "<p>Klant is toegevoegd.</p>";}?>
<div class="inner-container">
<div class="backend-form">
<form name="registration" method="post">
<h3><i class="fa fa-plus-circle"></i> Klant toevoegen</h3>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Naam</label>
<input type="text" name="klantNaam">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Soort klant</label>
<select class="" name="klantSoort">
<option value="Bedrijf">Bedrijf</option>
<option value="Particulier">Particulier</option>
</select>
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Telefoonnummer</label>
<input type="number" name="klantTel">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> E-mail adres</label>
<input type="email" name="klantEmail">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Straat & huisnr</label>
<input type="text" name="klantAdres">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Postcode</label>
<input type="text" name="klantPostcode">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Plaats</label>
<input type="text" name="klantPlaats">
</div>
<div class="form-input">
<input type="submit" name="submit" value="Klant toevoegen">
</div>
</form>
</div>
</div>
</div>
<footer>
<p>Copyright © 2018</p>
</footer>
</body>
</html>
Here's the working code. Glad you got it working!
<?php
include('auth.php');
if($_POST['submit'] == 'Klant toevoegen') {
require('db.php');
$stmt = $con->prepare('INSERT INTO `klanten` (klant_naam, klant_soort, klant_telefoon, klant_email, klant_straat, klant_postcode, klant_plaats) VALUES (?, ?, ?, ?, ?, ?, ?)');
if($stmt) {
$klant_naam = $_POST['klantNaam'];
$klant_soort = $_POST['klantSoort'];
$klant_telefoon = $_POST['klantTel'];
$klant_email = $_POST['klantEmail'];
$klant_adres = $_POST['klantAdres'];
$klant_postcode = $_POST['klantPostcode'];
$klant_plaats = $_POST['klantPlaats'];
$stmt->bind_param('ssissss', $klant_naam, $klant_soort, $klant_telefoon, $klant_email, $klant_adres, $klant_postcode, $klant_plaats);
$stmt->execute();
$con->close();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<!-- Charset, viewport en keywords -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Titel en favicon -->
<title></title>
<link rel="icon" href="./img/favicon.jpg">
<!-- Scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="./js/pageForward.js"></script>
<script type="text/javascript" src="./js/pageBack.js"></script>
<!-- Stylesheets en fonts -->
<link rel="stylesheet" href="./css/backend_styles.css">
<link href="https://fonts.googleapis.com/css?family=Work+Sans:200" rel="stylesheet"> <!-- font-family: 'Work Sans', sans-serif; -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300" rel="stylesheet"> <!-- font-family: 'Open Sans', sans-serif; -->
<link href="https://use.fontawesome.com/releases/v5.0.6/css/all.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="top-links">
<p><i class="fa fa-home"></i><i class="fa fa-cogs"></i><a onclick="goBack()"><i class="fa fa-arrow-left"></i></a><a onclick="goForward()"><i class="fa fa-arrow-right"></i></a> Backoffice | Ingelogt als: <?php echo $_SESSION['username']; ?> | Uitloggen</p>
</div>
<?php if($_POST['submit'] == 'Klant toevoegen') {echo "<p>Klant is toegevoegd.</p>";}?>
<div class="inner-container">
<div class="backend-form">
<form name="registration" method="post">
<h3><i class="fa fa-plus-circle"></i> Klant toevoegen</h3>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Naam</label>
<input type="text" name="klantNaam">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Soort klant</label>
<select class="" name="klantSoort">
<option value="Bedrijf">Bedrijf</option>
<option value="Particulier">Particulier</option>
</select>
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Telefoonnummer</label>
<input type="number" name="klantTel">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> E-mail adres</label>
<input type="email" name="klantEmail">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Straat & huisnr</label>
<input type="text" name="klantAdres">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Postcode</label>
<input type="text" name="klantPostcode">
</div>
<div class="form-input">
<label><i class="fa fa-info-circle"></i> Plaats</label>
<input type="text" name="klantPlaats">
</div>
<div class="form-input">
<input type="submit" name="submit" value="Klant toevoegen">
</div>
</form>
</div>
</div>
</div>
<footer>
<p>Copyright © 2018</p>
</footer>
</body>
</html>
I'm assuming that the HTML form is not part of addKlant.php?
In which case, once you've processed the form, you might want to redirect to the page with the HTML form, and pass an item in the querystring with some sort of variable to show the query has run.
i.e.
// ... after your form processing
$con->close();
header("Location: htmlpage.php?queryresult=ok");
exit;
and then in the HTML form page:
<?php if ($_GET['queryresult'] == 'ok') print "query ran okay"; ?>
I don't know what the result of the query might look like - if it is encodable within a url, you can pass this whole response, ie.
header("Location: htmlpage.php?queryresult=" . urlencode( $queryresultstring ));
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
$con = mysql_connect('localhost', 'root','');
mysql_select_db ('product');
echo "Connected to database";
session_start();
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Register</title>
<script type="text/javascript" src="assets/js/bootstrap.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
<link rel="stylesheet" href="reg.css" />
<!-- Google Fonts -->
<link href='https://fonts.googleapis.com/css?family=Passion+One' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Oxygen' rel='stylesheet' type='text/css'>
<script src="jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<table align="center">
<td>
<tr>
<div align="center" style="size:30%;" class="container">
<div class="row main" style="width:30%;">
<div class="panel-heading">
<div class="panel-title text-center">
<h1 class="title">Register Here</h1>
<hr />
</div>
</div>
<div class="main-login main-center">
<form class="form-horizontal" method="post" action="<?php
echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div align="left" class="form-group">
<label name="name" class="cols-sm-2 control-label">Your Name</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>
<input type="text" class="form-control" name="name" placeholder="Enter your Name"/>
</div>
</div>
</div>
<div align="left" class="form-group">
<label name="email" class="cols-sm-2 control-label">Your Email</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope fa" aria-hidden="true"></i></span>
<input type="text" class="form-control" name="email" placeholder="Enter your Email"/>
</div>
</div>
</div>
<div align="left" class="form-group">
<label name="user_name" class="cols-sm-2 control-label">Username</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-users fa" aria-hidden="true"></i></span>
<input type="text" class="form-control" name="user_name" placeholder="Enter your Username"/>
</div>
</div>
</div>
<div align="left" class="form-group">
<label name="password" class="cols-sm-2 control-label">Password</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock fa-lg" aria-hidden="true"></i></span>
<input type="password" class="form-control" name="password" placeholder="Enter your Password"/>
</div>
</div>
</div>
<div align="left" class="form-group">
<label name="confirm_password" class="cols-sm-2 control-label">Confirm Password</label>
<div class="cols-sm-10">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock fa-lg" aria-hidden="true"></i></span>
<input type="password" class="form-control" name="confirm_password" placeholder="Confirm your Password"/>
</div>
</div>
</div>enter code here
<div align="left" class="form-group ">
<button type="button" name="submit" class="btn btn-primary btn-lg btn-block login-button">Register</button>
</div>
<div class="login-register">
Already registered? Login here
</div>
</form>
</div>
</div>
</div>
</tr>
</td>
</table>
<?php
if (isset($_POST['submit']))
{
$name = $_POST['name'];
$username = $_POST['user_name'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['confirm_password'];
$slquery = "SELECT * FROM customer WHERE email = '$email'";
$selectresult = mysql_query($slquery);
if(empty($_POST['name']))
{
$nameErr='Enter Your Name!';
}
else
{
$user = test_input($_POST['name']);
if(!preg_match('/^[a-zA-Z0-9#_]*$/',$user))
{
$nameErr=' Re-Enter Your Name! Format Inccorrect!( only alpha, numbers,#_ are allowed)';
}
}
if(empty($_POST['password']))
{
$passErr='Enter Your Password!';
}
else
{
$user = test_input($_POST['password']);
if(!preg_match('/^[a-zA-Z0-9#_]*$/',$pass))
{
$passErr='Invalid Format! Re-Enter Password!';
}
}
if(mysql_num_rows($selectresult)>0)
{
$msg = 'email already exists';
}
elseif($password != $cpassword){
$msg = "passwords doesn't match";
}
else{
$query = "INSERT INTO customer (name,email,user_name, password,confirm_password, ) VALUES ('
$name', '$username', '$password', '$cpassword', '$email')";
$result = mysql_query($query);
if($result){
$msg = "User Created Successfully.";
}
}
}
?>
</body>
</html>
I am trying to create a registration form with validation.i used some bootstrap for the front end design then Mysql database along with php.
when I click to submit the registration form, it does not validate the field values.please, someone, tell me what's wrong with this.Also, it's not showing the errors. I don't know why this is not working.
Thank you in Advance.
Just change this line from
<button type="button" name="submit" class="btn btn-primary btn-lg btn-block login-button">Register</button>
to
<button type="submit" name="submit" class="btn btn-primary btn-lg btn-block login-button">Register</button>
You have used button so form is not submitted. Change it to submit type
EDIT
Note: please start using PDO or mysqli
Please check below point:
1) You have used test_input() but not defined in your shown code.
2) Your query have extra , after confirm_password.
3) Insert query will not work. There were some errors:
$query = "INSERT INTO customer (name,user_name, password,confirm_password, email) VALUES ('
$name', '$username', '$password', '$cpassword', '$email')";
4) No need to store confirm_password
Database
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "stackoverflow";
// Create connection
$conn = new mysqli($servername, $username, $password,$database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
session_start();
?>
Change the Register button like this
<div align="left" class="form-group ">
<button type="submit" name="submit" class="btn btn-primary btn-lg btn-block login-button">Register</button>
</div>
test_function
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
Full PHP
<?php
if (isset($_POST['submit']))
{
$name = $_POST['name'];
$username = $_POST['user_name'];
$email = $_POST['email'];
$password = $_POST['password'];
$cpassword = $_POST['confirm_password'];
$slquery = "SELECT * FROM customer WHERE email = '$email'";
$selectresult = mysqli_query($conn,$slquery);
if(empty($_POST['name']))
{
$nameErr='Enter Your Name!';
}
else
{
$user = test_input($_POST['name']);
if(!preg_match('/^[a-zA-Z0-9#_]*$/',$user))
{
$nameErr=' Re-Enter Your Name! Format
Incorrect!( only alpha, numbers,#_ are allowed)';
}
}
if(empty($_POST['password']))
{
$passErr='Enter Your Password!';
}
else
{
$user = test_input($_POST['password']);
if(!preg_match('/^[a-zA-Z0-9#_]*$/',$password))
{
$passErr='Invalid Format! Re-Enter Password!';
}
}
if (mysqli_num_rows($selectresult)>0)
{
echo 'email already exists';
}
elseif($password != $cpassword){
$msg = "passwords doesn't match";
}
else{
var_dump($name.$email.$username.$password.$cpassword);
$query = "INSERT INTO customer(name,email,user_name,
password,confirm_password) VALUES ('
$name', '$email','$username', '$password', '$cpassword')";
if ($conn->query($query) === TRUE) {
echo "New user created successfully";
} else {
echo "Error: " . $query . "<br>" . $conn->error;
}
$conn->close();
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}?>
I've been scratching my head for a hour now and can't figure out why php doesn't post my form values.
Code
<!-- Login -->
<div class="login__block toggled" id="l-login">
<div class="login__block__header">
<i class="zmdi zmdi-account-circle"></i>
Hi there! Please Sign in
<div class="actions login__block__actions">
<div class="dropdown">
<i class="zmdi zmdi-more-vert"></i>
<ul class="dropdown-menu pull-right">
<li><a data-block="#l-register" href="#">Create an account</a></li>
<li><a data-block="#l-forget-password" href="#">Forgot password?</a></li>
</ul>
</div>
</div>
</div>
<div class="login__block__body">
<div class="form-group form-group--float form-group--centered form-group--centered">
<input type="text" class="form-control" name="email">
<label>Email Address</label>
<i class="form-group__bar"></i>
</div>
<div class="form-group form-group--float form-group--centered form-group--centered">
<input type="password" class="form-control" name="password">
<label>Password</label>
<i class="form-group__bar"></i>
</div>
<button type="submit" class="btn btn--light btn--icon m-t-15"><i class="zmdi zmdi-long-arrow-right"></i></button>
</div>
<?php
if(isset($_POST['login'])) {
if(isset($_POST['email']) && isset($_POST['password']) &&
is_string($_POST['email']) && is_string($_POST['password']) &&
!empty($_POST['email']) && !empty($_POST['password'])) {
$email = stripslashes(strip_tags($_POST['email']));
$password = md5($_POST['password']);
$stmt = $pdo->prepare('SELECT * FROM users WHERE UserEmail = :UserEmail');
$stmt->bindParam(':UserEmail', $email);
$stmt->execute();
if($stmt->rowCount() > 0) {
$stmt = $pdo->prepare('SELECT * FROM users WHERE UserEmail = :UserEmail AND UserPassword = :UserPassword');
$stmt->execute(array(':UserEmail' => $email, ':UserPassword' => $password));
if($stmt->rowCount() > 0) {
$row = $stmt->fetch();
$UserLevel = $row['UserLevel'];
if($UserLevel == 'banned') {
$display->ReturnError('Your account has been suspended.');
return false;
}
$UserID = $row['UserID'];
$time = time();
$IPAddress = $_SERVER['REMOTE_ADDR'];
$_SESSION['auth'] = $UserID;
$stmt = $pdo->prepare('INSERT INTO logs (LogUserID, LogDate, LogIPAddress) VALUES (:LogUserID, :LogDate, :LogIPAddress)');
$stmt->execute(array(':LogUserID' => $UserID, ':LogDate' => $time, ':LogIPAddress' => $IPAddress));
$display->ReturnSuccess('You was successfully logged in.');
$settings->forceRedirect('index.php', 2);
} else {
$display->ReturnError('Invalid user credentials.');
}
} else {
$display->ReturnError('User with these credentials does not exists.');
}
}
}
?>
</div>
Note that I'm very new to PHP and it would mean the world to me if you could help me out! Thank you.
Submit Button only works when it is inside form tag.
Your code does not have any form tag
Try this
<form action="" >
<!-- Login -->
<!--your code here -->
</form>
add form attributes like this
<div class="login__block__body">
<form action="url which will process form" method="POST">
<div class="form-group form-group--float form-group--centered form-group--centered">
<input type="text" class="form-control" name="email">
<label>Email Address</label>
<i class="form-group__bar"></i>
</div>
<div class="form-group form-group--float form-group--centered form-group--centered">
<input type="password" class="form-control" name="password">
<label>Password</label>
<i class="form-group__bar"></i>
</div>
<button type="submit" class="btn btn--light btn--icon m-t-15"><i class="zmdi zmdi-long-arrow-right"></i></button>
</form>
</div>
All <input> tags must be within the <form> tags.
<form action="" method="post"> //Submits form to same page
<input/>
<input/>
<button type="submit" class="btn btn--light btn--icon m-t-15"><i class="zmdi zmdi-long-arrow-right"></i></button>
</form>
When you "submit" this form, it will POST all of the input values within the form, which you retrieve from the $_POST['name_of_input'] variables
I would recommend learning HTML before PHP.