PHP CODE REQUEST
Hey guys i want to asking about "How to keep the form value after we fail registration?"
this is my register.php :
<?php
ob_start();
session_start();
if( isset($_SESSION['user2'])!="" ){
header("Location: home.php");
}
include_once 'serverconfig.php';
if(isset($_POST['btn-signup'])) {
$alpha = "abcdefghijklmnopqrstuvwxyz";
$alpha_upper = strtoupper($alpha);
$numeric = "0123456789";
$special = ".-+=_,!#$#*%<>[]{}";
$chars = "";
if (isset($_POST['gencode'])){
// if you want a form like above
if (isset($_POST['alpha']) && $_POST['alpha'] == 'on')
$chars .= $alpha;
if (isset($_POST['alpha_upper']) && $_POST['alpha_upper'] == 'on')
$chars .= $alpha_upper;
if (isset($_POST['numeric']) && $_POST['numeric'] == 'on')
$chars .= $numeric;
if (isset($_POST['special']) && $_POST['special'] == 'on')
$chars .= $special;
$length = $_POST['length'];
}else{
// default [a-zA-Z0-9]{9}
$chars = $alpha . $alpha_upper . $numeric;
$length = 9;
}
$len = strlen($chars);
$gc = '';
for ($i=0;$i<$length;$i++)
$gc .= substr($chars, rand(0, $len-1), 1);
// the finished password
$gc = str_shuffle($gc);
$namalengkap_ = trim($_POST['namalengkap']);
$email_ = trim($_POST['email']);
$nomorhape_ = trim($_POST['nomorhape']);
$jeniskelamin_ = trim($_POST['jeniskelamin']);
$tanggallahir_ = trim($_POST['tanggallahir']);
$username_ = trim($_POST['uname']);
$password_ = trim($_POST['upass']);
$namalengkap = strip_tags($namalengkap_);
$email = strip_tags($email_);
$nomorhape = strip_tags($nomorhape_);
$jeniskelamin = strip_tags($jeniskelamin_);
$tanggallahir = strip_tags($tanggallahir_);
$username = strip_tags($username_);
$password = strip_tags($password_);
// check email exist or not
$query = "SELECT email FROM user2 WHERE email='$email'";
$result = mysql_query($query);
$query2 = "SELECT username FROM user2 WHERE username='$username'";
$result2 = mysql_query($query2);
$count = mysql_num_rows($result); // if email not found then proceed
$count2 = mysql_num_rows($result2);
if ($count==0 && $count2==0) {
$query = "INSERT INTO user2(namalengkap,email,jeniskelamin,tanggallahir,username,password,activecode,status) VALUES('$namalengkap','$email','$jeniskelamin','$tanggallahir','$username','$password','$gc','0')";
$res = mysql_query($query);
if ($res) {
$errTyp = "success";
$errMSG = "Registrasi Berhasil !";
} else {
$errTyp = "danger";
$errMSG = "Ada Kesalahan Saat Mengisi Form Di Bawah, Silahkan Cek Kembali";
}
} else {
$errTyp = "warning";
$errMSG = "E-Mail/Username Sudah Digunakan Oleh User Lain. Silahkan Daftar E-Mail, ";
$errMSG .= "Klik Disini";
}
}
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ariigoh Register</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
</head>
<body>
<div class="container">
<div id="login-form">
<form method="post" autocomplete="off">
<div class="col-md-12">
<div class="form-group">
<h2 class="">Sign Up to <kbd>Ariigoh Afnan Localhost</kbd></h2>
</div>
<div class="form-group">
<hr />
</div>
<?php
if ( isset($errMSG) ) {
?>
<div class="form-group">
<div class="alert alert-<?php echo ($errTyp==" success ") ? "success " : $errTyp; ?>">
<span class="glyphicon glyphicon-info-sign"></span>
<?php echo $errMSG; ?>
</div>
<div id="errmsgbox">
<div id="divError"></div>
</div>
</div>
<?php
}
?>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="namalengkap" class="form-control" placeholder="Masukan Nama Lengkap Anda" pattern="^[a-zA-Z ]*$" title="Masukan Nama Anda Yang Benar" max="32" min="4" required/>
</div>
<div class="input-group">
<p><kbd>Input tidak di perbolehkan nomor/spesial karakter</kbd></p>
</div>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-earphone"></span></span>
<input type="text" name="nomorhape" class="form-control" placeholder="Masukan Nomor Handphone Anda" pattern="^[0-9]*$" title="Masukan Nomor Handphone Yang Benar" max="14" min="4" required />
</div>
<div class="input-group">
<p><kbd>Input hanya nomor dan tanpa (+62) -> ex : 08xxxxxxxxxx</kbd> </p>
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span>
<input type="email" name="email" class="form-control" placeholder="Masukan Alamat Email Anda" pattern="^[A-Za-z#.0-9]*$" title="Masukan E-Mail yang Valid(Aktif)" required />
</div>
<div class="input-group">
<p><kbd>Input hanya diperbolehkan a-z(A-Z),#,0-9,dan.(titik) -> ex : contoh.12#localhost.com</kbd></p>
</div>
</div>
<div class="form-group">
<label for="sel1">Jenis Kelamin:</label>
<select class="form-control" id="sel1" name="jeniskelamin" required>
<option value="Laki-Laki">Laki-Laki</option>
<option value="Perempuan">Perempuan</option>
</select>
</div>
<div class="form-group">
<label for="sel2">Tanggal Lahir :</label>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
<input type="date" name="tanggallahir" class="form-control" required />
</div>
</div>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
<input type="text" name="uname" class="form-control" placeholder="Masukan Username" pattern="^[a-zA-Z0-9]*$" title="Masukan Username Dengan Benar" required />
</div>
<div class="input-group">
<p><kbd>Input hanya diperbolehkan a-z(A-Z) dan 0-9</kbd></p>
</div>
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
<input type="password" name="upass" class="form-control" placeholder="Masukan Password" pattern="^[a-zA-Z0-9]*$" title="Masukan Password Dengan Benar" required />
</div>
<div class="input-group">
<p><kbd>Input hanya diperbolehkan a-z(A-Z) dan 0-9</kbd></p>
</div>
</div>
<div class="form-group">
<div class="alert-danger">
<h4><strong>Perhatian !</strong> Gunakan E-Mail Yang Sudah Terdaftar di Localhost. Kalau Tidak Yakin Silahkan Daftar, Klik Disini</h4>
</div>
</div>
<div id="form-group">
<button type="submit" class="btn btn-block btn-primary" name="btn-signup">Sign Up</button>
</div>
<hr>
<div class="form-group">
Sign in Here...
</div>
</div>
</form>
</div>
</div>
</body>
</html>
what must i do with that script ?
thank you for you help :) and sorry for my bad english ;)
Use some thing like this:
<input type="email" name="email" class="form-control" placeholder="Masukan Alamat Email Anda" pattern="^[A-Za-z#.0-9]*$" title="Masukan E-Mail yang Valid(Aktif)" value="<?php isset($_REQUEST['email']) ? $_REQUEST['email'] : '' ?>" required />
this
value="<?php if(isset($_REQUEST['email'])) { echo $_REQUEST['email']; } ?>"
will retain value in the textbox on unsuccessful processing.
Take a look to this example may help you PHP Form validation
Related
I'm just wondering if anyone knows how to make the errors shown on this screenshot: https://imgur.com/a/eaTVR9g go underneath their dedicated input boxes like shown on this image: https://imgur.com/a/Sb1AfUj If anyone is kind enough to do it for me I would greatly appreciate it. Thank you!
Here is my code:
<?php
$title = "Register";
include ($_SERVER['DOCUMENT_ROOT'] . '/private/header.php');
if ($AUTH) {
header ('Location: /');
die();
}
if (isset($_POST['go'])) {
$username = $_POST['username'];
$email = strtolower($_POST['email']);
$password = $_POST['password'];
$passwordConfirm = $_POST['confirmPassword'];
$protectedPassword = password_hash($password, PASSWORD_ARGON2I);
// Validation Checks
$errors = array();
$Emailstmt = $db->prepare("SELECT * FROM `Users` WHERE `Email` = :email;");
$Emailstmt->bindParam(':email', $email, PDO::PARAM_STR);
$Emailstmt->execute();
if ($Emailstmt->rowCount() > 0) {
$error[] = 'The email you tried to use is already being used on an different account, please use another one.';
}
$Userstmt = $db->prepare("SELECT * FROM `Users` WHERE `Username` = :username;");
$Userstmt->bindParam(':username', $username, PDO::PARAM_STR);
$Userstmt->execute();
$checkIP = $db->prepare("SELECT count(*) FROM `Users` WHERE `LastIP` = :regIP");
$checkIP->bindParam(":regIP", $UserIP, PDO::PARAM_STR);
$checkIP->execute();
$checkIpAdress = $checkIP->fetchColumn();
if (empty($checkIpAdress)) {
$checkIpAdress = 0;
}
if ($checkIpAdress) {
if ($checkIpAdress > 3) {
array_push($errors, 'It looks like you have registered too many accounts under this IP address.');
}
}
if (strlen($username) < 3) {
array_push($errors, 'Your username must be at least 3 characters in total.');
}
if (strlen($password) < 5) {
array_push($errors, 'Your password must be at least 5 characters in total.');
}
if ($Userstmt->rowCount() > 0) {
array_push($errors, 'The username you tried to use is already being used, Maybe try to pick another one.');
}
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
//echo("$email is a valid email address");
} else {
array_push($errors, 'The email you specified(' . htmlspecialchars($email, ENT_QUOTES, "UTF-8") . ') is invaild.');
}
if (!preg_match("/^[a-zA-Z0-9][\w\.]+[a-zA-Z0-9]$/", $username)) {
array_push($errors, 'The username you specified(' . htmlspecialchars($username, ENT_QUOTES, "UTF-8") . ') contains special symbols or is invaild.');
}
if (strtolower($username) == strtolower($password)) {
array_push($errors, 'Your password can not be the same as your username.');
}
if ($password !== $passwordConfirm) {
array_push($errors, 'It looks like your passwords do not match.');
}
// Begin form submission
if (empty($errors)) {
$insert = $db->prepare("INSERT INTO `Users` (`Username`,`Email`,`Password`,`LastIP`,`TimeRegister`,`AvatarURL`) VALUES (:Username,:Email,:Password,:LastIP,:TimeRegister,:AvatarURL)");
$insert->bindParam(":Username", $username, PDO::PARAM_STR);
$insert->bindParam(":Email", $email, PDO::PARAM_STR);
$insert->bindParam(":Password", $protectedPassword, PDO::PARAM_STR);
$insert->bindParam(":LastIP", $UserIP, PDO::PARAM_STR);
$insert->bindParam(":TimeRegister", $now, PDO::PARAM_INT);
$insert->bindValue(":AvatarURL", '8ca17bec-0320-4293-90e5-dfc5b8690156', PDO::PARAM_STR);
$insert->execute();
?>
<div class="space">
<section class="hero is-success">
<div class="hero-body modal-button" data-target="modal" aria-haspopup="true"
style="padding: 1rem 1rem !important;">
<center>You have successfully registered! Please wait while we redirect you.</center>
</div>
</section><br>
</div>
<meta http-equiv='refresh' content='5;url=/auth/login' />
<?php
} else {
}
}
if ($SiteSettings->Registration == 0) {
echo '<section class="section">
<div class="container">
<div class="columns is-centered">
<div class="column is-7">
<div class="box">
<p>We\'re sorry, but account creation is currently disabled right now. Please try again later.</p>
</div>
</div>
</div>
</section>
';
include($_SERVER['DOCUMENT_ROOT'] . "/private/footer.php");
die;
}
?>
<section class="section">
<div class="container">
<div class="columns is-centered">
<div class="column is-7">
<div class="box">
<div class="title is-size-4">Register</div>
<form action="#" method="POST">
<input type="hidden" name="token" value="<?php echo $_SESSION["csrf_token"]; ?>" />
<div class="field">
<label class="label">Username</label>
<div class="control has-icons-left">
<input class="input" name="username" type="text" id="username" maxlength="15"
autocomplete="off" placeholder="Enter a username">
<span class="icon is-small is-left"><i class="fas fa-user"></i></span>
<p id="username_message"></p>
</div>
</div>
<div class="field">
<label class="label">E-Mail address</label>
<div class="control has-icons-left">
<input class="input" name="email" type="email" id="email" maxlength="128"
autocomplete="off" placeholder="Enter your e-mail address.">
<span class="icon is-small is-left"><i class="fas fa-envelope"></i></span>
<p id="email_message"></p>
</div>
</div>
<div class="field">
<label class="label">Password</label>
<div class="control has-icons-left">
<input class="input" name="password" type="password" id="password" maxlength="45"
autocomplete="off" placeholder="Enter your password.">
<span class="icon is-small is-left"><i class="fas fa-lock"></i></span>
<p id="password_message"></p>
</div>
</div>
<div class="field">
<label class="label">Confirm Password</label>
<div class="control has-icons-left">
<input class="input" name="confirmPassword" type="password" id="confirmPassword"
maxlength="45" autocomplete="off" placeholder="Confirm your password">
<span class="icon is-small is-left"><i class="fas fa-lock"></i></span>
<p id="confirmPassword_message"></p>
</div>
</div>
<div class="push-5"></div>
<button class="button is-success is-fullwidth" type="submit" name="go"><b>Register</b></button>
</form>
<?php
if (!empty($errors)) {
?>
<?php
foreach ($errors as $error) {
echo '<p class="help has-text-danger">' . $error . '</p>';
}
} ?>
</div>
<p class="has-text-centered">Already a member? Login</p>
</div>
</div>
</div>
</section>
<?php include ($_SERVER['DOCUMENT_ROOT'] . '/private/footer.php'); ?>
You can organize the array keys to reflect the fields they relate to, IE:
if (strlen($username) < 3) {
$errors['username'][] = 'Your username must be at least 3 characters in total.';
}
and then on the display side you can use said keys to identify what errors belong to what field, IE:
<div class="field">
<label class="label">Username</label>
<div class="control has-icons-left">
<input class="input" name="username" type="text" id="username" maxlength="15" autocomplete="off" placeholder="Enter a username">
<span class="icon is-small is-left"><i class="fas fa-user"></i></span>
<p id="username_message">
<?php if (isset($errors['username'])): ?>
<?php foreach($errors['username'] as $error): ?>
<?= $error ?> <br/>
<?php endforeach; ?>
<?php endif; ?>
</p>
</div>
I want to ask, what's wrong with the code that I made? I want to access the data of the user who is currently logged into the session so that it appears automatically filling out the checkout form. So users do not need to bother anymore to input data manually.
my checkout page error
I made it in native PHP, How to solve this? Thank you
This my checkout page COde
<div class="col-md-8 order-md-1">
<h4 class="mb-3">Detail Pembayaran</h4>
<form method="post" action="akun.php?c_id=<?php echo $id_member; ?>" name="checkout" class="needs-validation" novalidate>
<?php
if (isset($_SESSION['user'])) {
$id_member = $_SESSION['user'];
$get_member = "SELECT * FROM tbluser where id_member = '$id_member'";
$run_member = mysqli_query($con,$get_member);
if ($row_member = mysqli_num_rows($run_member)>0) {
$m_nama = $row_member['nama_member'];
$m_username = $row_member['username_member'];
$m_email = $row_member['email_member'];
$m_alamat = $row_member['alamat_member'];
}
}
?>
<div class="row">
<div class="col-md-12 mb-3">
<label >Nama Lengkap</label>
<input type="text" class="form-control" name="m_nama" value="<?php echo $m_nama ?>"
disabled >
<div class="invalid-feedback">
Nama Harus Valid.
</div>
</div>
</div>
<div class="mb-3">
<label>Username</label>
<div class="input-group">
<div class="input-group-prepend">
</div>
<input type="text" class="form-control" name="m_username" value="<?php echo $m_username ?>" disabled>
<div class="invalid-feedback" style="width: 100%;">
username harus diisi.
</div>
</div>
</div>
<div class="mb-3">
<label>Email <span class="text-muted"></span></label>
<input type="email" class="form-control" value="<?php echo $m_email ?>" disabled>
<div class="invalid-feedback">
Email harus diisi.
</div>
</div>
<div class="mb-3">
<label for="address">Alamat</label>
<input type="text" class="form-control" value="<?php echo $m_alamat ?>" disabled>
<div class="invalid-feedback">
Alamat Harus diisi.
</div>
</div>
<hr class="mb-4">
<h4 class="mb-3">Pembayaran</h4>
<div class="d-block my-3">
<div class="custom-control custom-radio">
<input id="cash" name="paymentMethod" type="radio" class="custom-control-input" checked
required>
<label class="custom-control-label" for="cash">Cash</label>
</div>
<div class="custom-control custom-radio">
<input id="transfer" name="paymentMethod" type="radio" class="custom-control-input"
disabled>
<label class="custom-control-label" for="transfer">Transfer (Soon)</label>
</div>
</div>
<hr class="mb-4">
<input class="btn btn-primary btn-lg btn-block mb-5" type="submit" name="submit" value="Masukkan Ke
Keranjang">
</form>
This is my Login page code(session declare)
<?php
session_start();
if (isset($_SESSION["login"])) {
header("Location: index-member.php");
}
include ("functions/functions.php");
include ("inc/db.php");
//cEK LOGIN TOMBOL SUDAH DI KLIK ATAU Belum
if (isset($_POST["login"])) {
$get_ip = getIpUser();
$id_member = "select * from tbluser where id_member = '$get_ip'";
$username_member = $_POST["m_username"];
$password_member = $_POST["m_password"];
$cek_username = mysqli_query($db, "SELECT * FROM tbluser WHERE username_member = '$username_member'");
// Cek Username ada sama atau tidak
if ( mysqli_num_rows($cek_username) === 1) {
//CEK PASSWORD
$row = mysqli_fetch_assoc($cek_username);
if( password_verify($password_member, $row["password_member"]) ) {
//Set Session
$_SESSION["login"] = true;
$_SESSION["user"] = $username_member;
header("Location: index-member.php");
exit;
}
}
$error = true;
}
?>
<?php
You store the value of $_POST['m_username'] in $_SESSION['user'], but then query for id_member = $_SESSION['user']...
$get_ip = getIpUser();
$id_member = "select * from tbluser where id_member = '$get_ip'";
$username_member = $_POST["m_username"];
...
$_SESSION["user"] = $username_member;
and
$id_member = $_SESSION['user'];
$get_member = "SELECT * FROM tbluser where id_member = '$id_member'";
I've created a signup.php file for html form and signup_form_handler.php for validate the html form. Before create a separate file for validation I tried to validate the html form in the same file (signup.php) and I succeeded. After I separate the validation part into another file I have this problem. How can i get back the validation errors from signup_form_handler.php to the html form file (signup.php) .
signup.php
<div class="container-fluid calbox col-md-4" style="margin-right: 10px;margin-bottom: 10px;">
<h2 style="text-align: center;color:#0275d8;">Sign Up Form</h2>
<form method="POST" action="signup_form_handler.php">
<div class="form-group">
<label style="padding-top: 10px;" class="col-form-label">Name</label>
<input type="text" class="form-control" placeholder="Type your name here" value="<?php if(isset($_POST['name'])){echo $_POST['name'];}?>" name="name" id="name">
<div class="errBox" style="padding-top: 5px;">
<?php echo $nameErr;?>
</div>
</div>
<div class="form-group">
<label class="col-form-label">Email</label>
<input type="email" class="form-control" placeholder="Type your email here" value="<?php if(isset($_POST['email'])){echo $_POST['email'];}?>" name="email" id="email">
<div class="errBox" style="padding-top: 5px;">
<?php echo $emailErr;?>
</div>
</div>
<div class="form-group">
<label class="col-form-label">City</label>
<select class="form-control" id="city" name="city[]">
<option value="Kandy">Kandy</option>
<option value="Colombo">Colombo</option>
<option value="Galle">Galle</option>
</select>
</div>
<fieldset class="form-group row">
<label class="col-form-label col-xs-4">Gender</label>
<div class="col-xs-7 form-check">
<label class="form-check-label">
<input type="radio" class="form-check-input" name="gender" id="genMale" value="Male" checked>
Male
</label>
<label class="form-check-label">
<input type="radio" class="form-check-input" name="gender" id="genFemale" value="Female">
Female
</label>
</div>
</fieldset>
<div class="form-group">
<label class="col-form-label col-xs-4">Interested Areas</label>
<div class="col-xs-7 form-check">
<label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0 offset-xs-1">
<input type="checkbox" name="check[]" id="web" class="custom-control-input" value="Web Design">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Web Design</span>
</label>
<label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0">
<input type="checkbox" name="check[]" id="gd" class="custom-control-input" value="Graphic Design">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Graphic Design</span>
</label>
<label class="custom-control custom-checkbox mb-2 mr-sm-2 mb-sm-0">
<input type="checkbox" name="check[]" id="se" class="custom-control-input" value="Software Engineering">
<span class="custom-control-indicator"></span>
<span class="custom-control-description">Software Engineering</span>
</label>
</div>
</div>
<div class="errBox" style="padding-top: 5px;">
<?php echo $checkErr;?>
</div>
<div class="form-group">
<label class="col-form-label">Phone Number</label>
<input type="tel" class="form-control" placeholder="Type your phone number here" value="<?php if(isset($_POST['phone'])){echo $_POST['phone'];}?>" name="phone" id="phone">
</div>
<div class="form-group">
<label class="col-form-label">Password</label>
<input type="password" class="form-control" placeholder="Type a password here" name="pass" id="pass">
<div class="errBox" style="padding-top: 5px;">
<?php echo $passErr;?>
</div>
</div>
<div class="form-group">
<label class="col-form-label">Confirm Password</label>
<input type="password" class="form-control" placeholder="Retype the password here" name="cpass" id="cpass">
<div class="errBox" style="padding-top: 5px;">
<?php echo $cpassErr;?>
</div>
</div>
<div class="form-group row offset-sm-8" style="padding-left: 10px;">
<button type="submit" class="btn btn-outline-primary" name="btn" value="signup" id="signupbtn">Register</button>
</div>
</form>
</div>
signup_form_handler.php
<?php
$nameErr = $emailErr = $checkErr = $passErr = $cpassErr = "";
$name = $email = $pass = $check = $checklist = $value = $gender = $city = $phone = $dateTime = "";
if (isset($_POST['btn'])) {
if (empty($_POST["name"])) {
$nameErr = '<div class="alert alert-danger">Name is required !</div>';
} else{
$name = test_validate($_POST['name']);
}
if (empty($_POST["email"])) {
$emailErr = '<div class="alert alert-danger">Email is required !</div>';
}else{
$email = test_validate($_POST['email']);
}
if (isset($_POST['phone'])) {
$phone = test_validate($_POST['phone']);
}
if (empty($_POST["pass"])) {
$passErr = '<div class="alert alert-danger">Password is required !</div>';
} elseif (!empty($_POST["pass"]) < 6) {
$passErr = '<div class="alert alert-danger">Minimum 6 characters required !</div>';
}
if (empty($_POST["cpass"])) {
$cpassErr = '<div class="alert alert-danger">Confirm password is required !</div>';
} elseif ($_POST["pass"] != $_POST["cpass"]) {
$cpassErr = '<div class="alert alert-danger">Password fields do not match !</div>';
}else{
$pass = test_validate($_POST['pass']);
}
if (empty($_POST['check'])) {
$checkErr = '<div class="alert alert-danger">You should at least select 1 area that you are interested !</div>';
}
if (isset($_POST['gender'])) {
$gender = test_validate($_POST['gender']);
}
if (isset($_POST['city'])) {
foreach ($_POST['city'] as $citylist) {
$city = $citylist;
}
}
date_default_timezone_set("Asia/Colombo");
$dateTime = date("Y-m-d h:i:s a");
}
function test_validate($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
in signup_form_handler.php
SESSION_START();
SESSION['data'] = $data;
in signup.php
SESSION_START();
print SESSION['data'];
Well you can save the error message in a session variable. In case of error you can redirect the user back to the signup.php page. Try following code in signup_form_handler.php. It should go at the bottom of the first If clause
$error_message = "";
if ($nameErr != "") $error_message = $nameErr;
if ($emailErr != "") $error_message = $emailErr;
if ($checkErr != "") $error_message = $checkErr;
if ($passErr != "") $error_message = $passErr;
if ($cpassErr != "") $error_message = $cpassErr;
if ($error_message != "") {
$_SESSION['error_message'] = $error_message;
header("Location: " . $error_message);
exit;
}
Also add the line : session_start(); at the top of the signup_form_handler.php and signup.php.
In signup.php file, you have to add the lines:
if (isset($_SESSION['error_message']) && $_SESSION['error_message'] != "") {
echo $_SESSION['error_message'];
unset($_SESSION['error_message']);
}
I have a URL link, and upon clicking the link, it has to populate the fields as soon as the page is opened, below is the URL link and the form that has to be populated with few fields,fields such as names, idocode and email has to be populated, and the rest by the user and the submit button has no effect, what am I doing wrong,
URL: 'http://www.domain.com/register/registration.php?token=$token&stud_id=stud_id'
<?php
error_reporting(1);
session_start();
include 'includes/connect.php';
$student_id = $_GET["id"];
$_SESSION['student_id'] = $student_id;
$token = $_GET["tokenk"];
$_SESSION['token'] = $token;
if (isset($_GET["id"]) && isset($_GET["tk"])) {
$sql = "SELECT * FROM student WHERE student_id=" . $student_id . " and token='" . $token . "'";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if (!empty($result)) {
$sql = "UPDATE student SET first_name=:first_name, middle_name=:middle_name, last_name=:last_name WHERE student_id=:student_id";
$stmt->bindValue(':first_name', $first_name);
$stmt->bindValue(':middle_name', $middle_name);
$stmt->bindValue(':last_name', $last_name);
$stmt->bindValue(':student_id', $student_id);
$result = $stmt->execute();
} else {
}
}
if ($_POST["Submit"] == "Submit") {
//echo '<pre>';print_r($_POST["name"]);exit;
$ccode = $_POST["idode"];
$first_name = $_POST['first_name'];
$middle_name = $_POST['middle_name'];
$last_name = $_POST['last_name'];
$course = $_POST["course"];
$email = $_POST['email'];
$password = $_POST["password"];
$confirm_password = $_POST["confirm_password"];
//echo "<br/>In post"; die();
//echo $name.'>>>>'.$email.'>>>>'.$idcode;exit;
add_student_session($_POST);
$validate = add_student_validator($_POST);
//echo '<pre>';print_r($validate);exit;
if (isset($validate) && $validate) {
//echo "<br/>Validated: TRUE<br/>"; die();
$sql = "SELECT COUNT(*) AS num FROM student WHERE student_id = :student_id";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':idcode', $idcode);
$stmt->bindValue(':first_name', $first_name);
$stmt->bindValue(':middle_name', $middle_name);
$stmt->bindValue(':last_name', $last_name);
$stmt->bindValue(':course', $course);
$stmt->bindValue(':date_of_birth', $date_of_birth);
$stmt->bindValue(':email', $email);
$stmt->bindValue(':password', $password);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($row['num'] > 0) {
$_SESSION["already_exist"] = TRUE;
} else {
//echo "<br/>Validated: FALSE<br/>"; die();
$sql = "INSERT INTO student (course, date_of_birth, password) VALUES (:course, :date_of_birth, :password)";
$stmt = $pdo->prepare($sql);
//echo "<br/>CCODE: ".$idcode;exit;
$stmt->bindValue(':course', $course);
$stmt->bindValue(':date_of_birth', $date_of_birth);
$stmt->bindValue(':password', $password);
$stmt->bindValue(':confirm_password', $confirm_password);
if ($_POST["password"] != $_POST["confirm_password"]) {
echo 'passwords do not match';
}
$result1 = $stmt->execute();
//echo $result ;exit;
if ($result1) {
echo '<div><p class="text-success">Created Successfully!</p></>';
}
clear_session();
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"/>
<link rel="stylesheet" href="css/bootstrap.min.custom.css" type="text/css"/>
<script src="js/bootstrap.min.js"></script>
<script language="javascript" src="js/calendar.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="Absolute-Center is-Responsive">
<div class="col-md-10 col-md-offset-2 well">
<form class="form-horizontal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"
role="form">
<legend>Registration</legend>
<div class="form-group">
<label for="name" class="col-xs-2 control-label">CCODE:</label>
<div class="col-xs-10">
<input class="form-control" type="text" name="idcode" required
placeholder="CCODE/EMPLOYEE CODE/ ID CODE"
value="<?php echo #$_POST['idcode']; ?>" readonly/>
<label for="idcode" generated="true" class="error">
<?= isset($error_hash["idcode"]) ? $error_hash["idcode"] : "" ?>
</label>
</div>
</div>
<div class="form-group">
<label for="birthday" class="col-xs-2 control-label">Name:</label>
<div class="col-xs-10">
<div class="form-inline">
<div class="form-group">
<input class="form-control" type="text" name="first_name" required
placeholder="First Name"
value="<?php echo #$_POST['first_name']; ?>"/>
<label for="first_name" generated="true" class="error">
<?= isset($error_hash["first_name"]) ? $error_hash["first_name"] : "" ?>
</label>
</div>
<div class="form-group">
<input class="form-control" type="text" name="middle_name" required
placeholder="Middle Name"
value="<?php echo #$_POST['middle_name']; ?>"/>
<label for="middle_name" generated="true" class="error">
<?= isset($error_hash["middle_name"]) ? $error_hash["middle_name"] : "" ?>
</label>
</div>
<div class="form-group">
<input class="form-control" type="text" name="last_name" required
placeholder="Last Name" value="<?php echo #$_POST['last_name']; ?>"/>
<label for="middle_name" generated="true" class="error">
<?= isset($error_hash["last_name"]) ? $error_hash["last_name"] : "" ?>
</label>
</div>
</div>
</div>
</div>
<div class="form-group">
<label for="name" class="col-xs-2 control-label">Designation:</label>
<div class="col-xs-10">
<input class="form-control" type="text" name="course" required
placeholder="Designation"
value="<?= isset($_SESSION["course"]) ? $_SESSION["course"] : ""; ?>"/>
<label for="course" generated="true" class="error">
<?= isset($error_hash["course"]) ? $error_hash["course"] : "" ?>
</label>
</div>
</div>
<div class="form-group">
<label for="name" class="col-xs-2 control-label">Date-Of-Birth:</label>
<div class="col-xs-8">
<select name="dd">
<option value="">Date</option>
<?php
for ($i = 1; $i <= 31; $i++) {
echo "<option value='$i'>" . $i . "</option>";
}
?>
</select>
<select name="mm">
<option value="">Month</option>
<?php
for ($i = 1; $i <= 12; $i++) {
echo "<option value='$i'>" . $i . "</option>";
}
?>
</select>
<select name="yy">
<option value="">Year</option>
<?php
for ($i = 1960; $i <= 2020; $i++) {
echo "<option value='$i'>" . $i . "</option>";
}
?>
</select>
</div>
</div>
<div class="form-group">
<label for="name" class="col-xs-2 control-label">Email:</label>
<div class="col-xs-10">
<input class="form-control" type="email" name="email" required placeholder="Email"
value="<?php echo #$_POST['email']; ?>" readonly/>
<label for="email" generated="true" class="error">
<?= isset($error_hash["email"]) ? $error_hash["email"] : "" ?>
</label>
</div>
</div>
<div class="form-group">
<label for="name" class="col-xs-2 control-label">Password:</label>
<div class="col-xs-10">
<input class="form-control" type="password" name="password" required
placeholder="Password"
value="<?= isset($_SESSION["password"]) ? $_SESSION["password"] : ""; ?>"/>
<label for="name" generated="true" class="error">
<?= isset($error_hash["password"]) ? $error_hash["password"] : "" ?>
</label>
</div>
</div>
<div class="form-group">
<label for="name" class="col-xs-2 control-label">Confirm Password:</label>
<div class="col-xs-10">
<input class="form-control" type="password" name="confirm_password" required
placeholder="Confirm Password"
value="<?= isset($_SESSION["confirm_password"]) ? $_SESSION["confirm_password"] : ""; ?>"/>
<label for="name" generated="true" class="error">
<?= isset($error_hash["confirm_password"]) ? $error_hash["confirm_password"] : "" ?>
</label>
</div>
</div>
</form>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="form-actions">
<input type="submit" name="Submit" value="Submit" class="btn btn-primary"/>
</div>
<br>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</html>
PHP Code
<?php
if (!isset($_SESSION)) { session_start(); }
include "connect.php";
include "functions.php";
if (!isset($_SESSION['login']) || $_SESSION['login'] !== true) {
header('location: no_acces.php');
exit();
} else {
$id_user = $_SESSION['userid'];
$q_user = mysqli_query($conn, "SELECT * FROM users WHERE id = $id_user");
if (mysqli_num_rows($q_user) === 1) {
$r_user = mysqli_fetch_assoc($q_user);
} else {
unset($_SESSION['login']);
unset($_SESSION['userid']);
header('location: no_acces.php');
exit();
}
}
$error = "";
$userQuery = mysqli_query($conn, "SELECT username FROM users");
$user = mysqli_fetch_assoc($userQuery);
$id = $_GET['id'];
if (isset($_POST['edit_contact'])) {
$roepnaam = $_POST['roepnaam'];
$naam = $_POST['naam'];
$land = $_POST['land'];
$bedrijf = $_POST['bedrijf'];
$adres1 = $_POST['adres1'];
$adres2 = $_POST['adres2'];
$stad = $_POST['stad'];
$postcode = $_POST['postcode'];
$provincie = $_POST['provincie'];
$telefoon = $_POST['telefoon'];
$email = $_POST['email'];
$captcha= $_POST['g-recaptcha-response'];
if(!$captcha){
$error = "Er is een fout opgetreden";
}
if ($error == "") {
$insertUser = ("UPDATE address SET
roepnaam = '$roepnaam', naam = '$naam', bedrijf = '$bedrijf', telefoon = '$telefoon', email = '$email', adres1 = '$adres1', adres2 = '$adres2', stad = '$stad', postcode = '$postcode', provincie = '$provincie', land = '$land' WHERE id = $id");
if (mysqli_query($conn, $insertUser)) {
$_SESSION['edit_contact'] = true;
header('location: address_book.php');
} else {
$error = "Er is een fout opgetreden";
}
}
}
?>
HTML Code
<!DOCTYPE html>
<html lang="en">
<body>
<form action="" method="post">
<?php if ($error !== "") { ?>
<div class="row">
<div class="col-md-12 error">
<?php echo $error; ?>
</div>
</div>
<?php } ?>
<label for="firstName" class="control-label">Naam:</label>
<div class="row ">
<div class="col-md-6">
<input type="text" class="form-control" id="firstName" placeholder="Roepnaam" name="roepnaam" value="<?php if (isset($_POST['roepnaam'])) { echo $_POST['roepnaam']; } ?>" required/>
</div>
<div class="col-md-6">
<input type="text" class="form-control" id="lastName" placeholder="Naam" name="naam" value="<?php if (isset($_POST['naam'])) { echo $_POST['naam']; } ?>" required/>
</div>
</div>
<label for="username" class="control-label">Bedrijf:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="username" placeholder="Bedrijf" name="bedrijf" value="<?php if (isset($_POST['bedrijf'])) { echo $_POST['bedrijf']; } ?>" required/>
</div>
</div>
<label for="password" class="control-label">Telefoonnummer:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="password" placeholder="Telefoonnummer" name="telefoon" value="<?php if (isset($_POST['telefoon'])) { echo $_POST['telefoon']; } ?>" required/>
</div>
</div>
<label for="email" class="control-label">Email:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="email" placeholder="E-mailadres" name="email" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } ?>" required/>
</div>
</div>
<label for="adres1" class="control-label">Adres:</label>
<div class="row">
<div class="col-md-12">
<input type="text" class="form-control" id="adres1" placeholder="Adres 1" name="adres1" value="<?php if (isset($_POST['adres1'])) { echo $_POST['adres1']; } ?>" required/>
</div>
</div>
<div class="row padding-top-10">
<div class="col-md-12">
<input type="text" class="form-control" id="adres2" placeholder="Adres 2" name="adres2" value="<?php if (isset($_POST['adres2'])) { echo $_POST['adres2']; } ?>"/>
</div>
</div>
<div class="row">
<div class="col-md-3">
<label for="postcode" class="control-label">Postcode:</label>
</div>
<div class="col-md-5">
<label for="city" class="control-label">Stad:</label>
</div>
<div class="col-md-4">
<label for="regio" class="control-label">Regio:</label>
</div>
</div>
<div class="row ">
<div class="col-md-3">
<input type="text" class="form-control" id="postcode" placeholder="Postcode" name="postcode" value="<?php if (isset($_POST['postcode'])) { echo $_POST['postcode']; } ?>" required/>
</div>
<div class="col-md-5">
<input type="text" class="form-control" id="city" placeholder="Stad" name="stad" value="<?php if (isset($_POST['stad'])) { echo $_POST['stad']; } ?>" required/>
</div>
<div class="col-md-4">
<input type="text" class="form-control" id="regio" placeholder="Provincie" name="provincie" value="<?php if (isset($_POST['provincie'])) { echo $_POST['provincie']; } ?>" required/>
</div>
</div>
<label for="land" class="control-label">Land:</label>
<div class="row ">
<div class="col-md-12">
<input type="text" class="form-control" id="password" placeholder="Land" name="land" value="<?php if (isset($_POST['land'])) { echo $_POST['land']; } ?>" required/>
</div>
</div>
<div class="row">
<div class="col-md-8 padding-top-10 ">
<div class="g-recaptcha " data-sitekey="6LcCsBoTAAAAAK72uzyJSrgWwD8xuF6jFIfgFaHX"></div>
</div>
</div>
<div class="row">
<div class="col-md-2 padding-top-10">
<input type="submit" name="edit_contact" class="btn btn-succes" value="Wijzigen">
</div>
<div class="col-md-2 padding-top-10">
<input type="text" name="delete_contact" action="delete_contact.php" class="btn btn-succes" value="Contact verwijderen">
</div>
</div>
</form>
</body>
</html>
PHP Code
<?php
if (!isset($_SESSION)) { session_start(); }
include "connect.php";
include "functions.php";
if (!isset($_SESSION['login']) || $_SESSION['login'] !== true || !isset($_SESSION['userid']) || $_SESSION['userid'] == "") {
header('location: login.php');
exit();
} else {
session_regenerate_id();
}
$id = $_GET['id'];
$query = "DELETE FROM address WHERE id= $id";
mysqli_query ($query);
if (mysql_affected_rows() == 1) {
header('location: addressbook.php');
} else {
echo "Verwijderen mislukt";
}
?>
I'm trying to make a delete button for my contacts within the addressbook. but everytime I click "Contact verwijderen" the webpage resets it self and the contact won't be deleted. Could anyone help me to fix this?
You input is a text input and you don't have a form asociated with it,create one and change the type of submit to submit
<form action="delete_contact.php" method="post">
//other inputs
<input type="submit" name="delete_contact" class="btn btn-succes" value="Contact verwijderen">
</form>
You are mixing MySQL and MySQLi functions:
mysqli_query ($query);
if (mysql_affected_rows() == 1)
You cannot mix MySQL with MySQLi, your code should be:
mysqli_query ($query);
if (mysqli_affected_rows($conn) == 1)
Add a normal link to delete the contact, you don't need a form.
<a href="delete_contact.php?id=<?php echo $id ?>">
Contact verwijderen
</a>