I want to set default user ( Trainees role ) for newly registered user on my system.
My main problem is registration works but the 'role' tab on the database is empty. Tried adding dropdown button for trainees role but the 'role' tab on the users table is still empty.
Is it allowed to set default user role upon user registration?
Database Image
Registration.php code
<?php
// Define variables and initialize with empty values
$username = $password = $confirm_password = "";
$username_err = $password_err = $confirm_password_err = "";
require_once "php/dbconnect.php";
if(isset($_SESSION['user_id'])!="") {
header("Location: index.php");
}
if (isset($_POST['signup'])) {
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
$name = mysqli_real_escape_string($conn, $_POST['name']);
$emailid = mysqli_real_escape_string($conn, $_POST['emailid']);
$age = mysqli_real_escape_string($conn, $_POST['age']);
$location = mysqli_real_escape_string($conn, $_POST['location']);
$role = mysqli_real_escape_string($conn, $_POST['role']);
if (!preg_match("/^[a-zA-Z ]+$/",$name)) {
$name_error = "Name must contain only alphabets and space";
}
if(!filter_var($emailid,FILTER_VALIDATE_EMAIL)) {
$email_error = "Please Enter Valid Email ID";
}
if(strlen($password) < 6) {
$password_error = "Password must be minimum of 6 characters";
}
if(strlen($mobile) < 10) {
$mobile_error = "Mobile number must be minimum of 10 characters";
}
if($password != $cpassword) {
$cpassword_error = "Password and Confirm Password doesn't match";
}
if(mysqli_query($conn, "INSERT INTO user(username,password,name,emailid,age,location,role) VALUES('" . $username . "', '" . md5($password) . "', '" . $name . "', '" . $emailid . "','" . $age . "','" . $location . "','" . $role . "')")) {
header("location:http://localhost/test/?url=users_index");
exit();
} else {
$error = 'Invalid Username or Password';
}
mysqli_close($conn);
}
?>
Registration Form:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>G&L Management System</title>
<!-- BOOTSTRAP STYLES-->
<link href="css/bootstrap.css" rel="stylesheet" />
<!-- FONTAWESOME STYLES-->
<link href="css/font-awesome.css" rel="stylesheet" />
<!-- GOOGLE FONTS-->
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css' />
<style>
#font-face {
font-family: Poppins;
src: url("fonts/Poppins-Regular.ttf");
}
html * {
font-family: "Poppins", sans-serif;
}
.myhead{
margin-top:0px;
margin-bottom:0px;
text-align:center;
}
</style>
</head>
<body>
<center>
<img src="banner.png" style="width: 300px; height: 250px;">
</center>
<div class="container">
<div class="row ">
<div class="col-md-4 col-md-offset-4 col-sm-6 col-sm-offset-3 col-xs-10 col-xs-offset-1">
<div class="panel-body" style="background-color: #E2E2E2; margin-top:70px; box-shadow: 5px 10px #888888;">
<h3 class="myhead">User Registration</h3>
<form role="form" action="register.php" method="post">
<br>
<label>Username</label>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa fa-user" ></i><?php if (isset($username_error)) echo $username_error; ?></span>
<input type="text" class="form-control" placeholder="Username" name="username" required />
</div>
<label>Password</label>
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa fa-lock" ></i><?php if (isset($password_error)) echo $password_error; ?></span>
<input type="password" class="form-control" placeholder="Password" name="password" required />
</div>
<label>Name</label>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa fa-user" ></i><?php if (isset($name_error)) echo $name_error; ?></span>
<input type="text" class="form-control" placeholder="Name" name="name" required />
</div>
<label>Email</label>
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa fa-envelope" ></i><?php if (isset($emailid_error)) echo $emailid_error; ?></span>
<input type="text" class="form-control" placeholder="Email" name="emailid" required />
</div>
<label>Age</label>
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa fa-child" ></i><?php if (isset($age_error)) echo $age_error; ?></span>
<input type="number" class="form-control" placeholder="Age" name="age" required />
</div>
<label>Location</label>
<div class="form-group input-group">
<span class="input-group-addon"><i class="fa fa-map-marker" ></i><?php if (isset($location_error)) echo $location_error; ?></span>
<input type="text" class="form-control" placeholder="Location" name="location" required />
</div>
<div>
<input type="submit" class="btn btn-success" name="signup" value="Submit">
<div class="form-group text-center">
<div class="col-sm-12 mt-3">
Already have an account?
<p>Login Here</p>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
You can simply set default value for column "role" to trainee, that way whenever new user is created it is assigned role of trainee automatically.
ALTER TABLE `user`
ALTER `role` SET DEFAULT "trainees";
otherwise you can add drop down like this
<label for="role">Choose a role:</label>
<select id="role" name="role">
<option value="admin">Admin</option>
<option value="coach">Coach</option>
<option value="trainees">Trainees</option>
</select>
Related
<!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 create form which contain text fields and upload file(register.php), and I want to send data to sendData.php and redirect to payment.php in order user to make payment for document review. I don't know where am wrong from sendData.php, I try to submit data from register.php to sendData and output is blank no error found, I try to check mysql error also no error found.
Page contain Form data
<?php
$errors = "";
if(isset($_GET["error"])){
if($_GET["error"] === "pwd"){
$errors = '<div class="alert alert-danger col-md-3><span class="glyphicon glyphiocn-remove-circle"></span> Passowrd dismatch, please try again</div>';
}
if($_GET["error"] === "undefined"){
$errors = '<div class="alert alert-danger col-md-3><span class="glyphicon glyphiocn-remove-circle"></span> File format dismatch, please try again</div>';
}
if($_GET["error"] === "found_file"){
$errors = '<div class="alert alert-danger col-md-3><span class="glyphicon glyphiocn-remove-circle"></span> Document found error, please try again</div>';
}
}
?>
<!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">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
<title>CentralAcademy: Register</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<link href="css/bootstrap-theme.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link rel="stylesheet" href="css/jumbtron.css">
<link href="css/custom-file.css" rel="stylesheet">
<style type="text/css">
body{ padding-top: 80px; }
</style>
</head>
<body>
<?php require_once("includes/header.php"); ?>
<div class="container">
<div class="row">
<div class="col-lg-3 breadcrumb">
<h3 class="page-header">Payment</h3>
<p><img src="images/images.png" class="img img-responsive" width="70" height="50" style="display: block; float: left; padding-right:10px;"/> (+255)755-555-555</p><br>
<p><img src="images/airtelM.fw.png" class="img img-responsive" width="70" height="50" style="display: block; float: left; padding-right:10px;"/> (+255)685-555-555</p><br>
<p><img src="images/tigopesa.png" class="img img-responsive" width="70" height="50" style="display: block; float: left; padding-right:10px;"/> (+255)655-555-555</p><br>
</div>
<div class="col-lg-2"></div>
<div class="col-lg-7 breadcrumb">
<?php print $errors; ?>
<h3 class="page-header">Request for Document Review</h3>
<h5><font color="#FF0000">***</font> Please fill all fields required</h5><br>
<form action="sendData.php" method="post" enctype="multipart/form-data" class="form-horizontal">
<div class="form-group">
<div class="col-md-6">
<input name="fname" type="text" placeholder="Enter your FirstName" value="<?php echo isset($_POST['fname']) ? $_POST['fname'] : '' ?>" class="form-control" required id="fname">
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<input name="sname" type="text" class="form-control" value="<?php echo isset($_POST['sname']) ? $_POST['sname'] : '' ?>" placeholder="Enter your LastName" required id="sname">
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<input name="gender" type="radio" class="radio-inline" value="F" checked="CHECKED" required/>Female <input type="radio" name="gender" class="radio-inline" value="M" required/>Male</div>
</div>
<div class="form-group">
<div class="col-md-6">
<input name="email" type="text" class="form-control" value="<?php echo isset($_POST['email']) ? $_POST['email'] : '' ?>" placeholder="Enter your Email Address" required id="email">
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<input name="phone" type="text" class="form-control" placeholder="Enter your Phone Number" value="<?php echo isset($_POST['phone']) ? $_POST['phone'] : '' ?>" maxlength="13" required id="phone" pattern="[\+]\d{3}\d{3}\d{6}">
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<input type="radio" name="edlevel" value="bachelor" class="radio-inline" checked="CHECKED" required/>Bachelor
<input type="radio" name="edlevel" value="Post Graduate" class="radio-inline" />Post Grad
<input name="edlevel" type="radio" value="Masters" class="radio-inline" />Masters
<input name="edlevel" type="radio" value="PhD" class="radio-inline" />PhD
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<input name="affi" type="text" class="form-control" value="<?php echo isset($_POST['affi']) ? $_POST['affi'] : '' ?>" placeholder="Enter organisation/College/University" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<input name="title" type="text" class="form-control" value="<?php echo isset($_POST['title']) ? $_POST['title'] : '' ?>" placeholder="Enter your document title" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 fileUpload">
<input type="file" name="files" id="file"/>
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<input name="password" type="password" class="form-control" placeholder="Choose Your Password" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<input name="password2" type="password" class="form-control" placeholder="Confirm Your Password" required>
</div>
</div>
<br>
<div class="form-group">
<div class="col-md-6">
<button name="Register" type="submit" class="btn btn-info"><span class="glyphicon glyphicon-user"></span> Register Now</button><br><br>
<p>If you are already registered please click here</p>
</div>
</div>
</form>
</div>
</div>
</div>
<div class="container marketing">
<!-- START THE FEATURETTES -->
<?php require_once("includes/footer.php"); ?>
</div><!-- /.container -->
<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>
//sendData.php
<?php
if(isset($_FILES['files']) && $_POST["password"] === $_POST["password2"]){
session_start();
require_once("includes/dbconnect.php");
$fname = $_POST["fname"];
$sname = $_POST["sname"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$title = $_POST["title"];
$password = $_POST["password"];
$gender = $_POST["gender"];
$edlevel = $_POST["edlevel"];
$affi = $_POST["affi"];
//Keep all author data to session variable
$_SESSION["fname"] = $fname;
$_SESSION["sname"] = $sname;
$_SESSION["email"] = $email;
$_SESSION["phone"] = $email;
$_SESSION["passwprd"] = $password;
$_SESSION["gender"] = $gender;
$_SESSION["edlevel"] = $edlevel;
$_SESSION["affi"] = $affi;
$errors= array();
$extensions = array("docx");
$file_name = rand(1000,100000)."-".$_FILES['files']['name'];
$file_size = $_FILES['files']['size'];
$file_tmp = $_FILES['files']['tmp_name'];
$file_type = $_FILES['files']['type'];
$file_ext = strtolower(substr($_FILES['files']['name'],strrpos($_FILES['files']['name'],'.')+1));
if(in_array($file_ext,$extensions ) === true && $file_size < 2097152){
$result = $connect->query("INSERT INTO temp VALUES(NULL,'$title',$email','$file_name',now())");
if($result){
$desired_dir="AuthorReport";
if(empty($errors)===true){
if(is_dir($desired_dir)===false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"$desired_dir/".$file_name);
}else{ // rename the file if another one exist
$new_dir="$desired_dir/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
header("location: payment.php");
exit();
}
else{ header("location:register.php?error=found_file"); exit(); }
}
}
else{ header("location:register.php?error=undefined"); exit(); }
}
else{ header("location:register.php?error=pwd"); exit(); }
?>
$email doesn't have start quote
$result = $connect->query("INSERT INTO temp VALUES(NULL,'$title',$email','$file_name',now())");
that's why you have the error.
and to be sure where are you entering these values exactly where you want use add also the column names in INSERT statement
$result = $connect->query("INSERT INTO temp
(column_title, column_email, column_filename, thedate)
VALUES
('$title','$email','$file_name',NOW() )" );
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
Whenever I fill the email and password tabs, then press the login button, nothing seems to happen nothing loads or gets submitted. However I leave both email and passwords tabs blank, then press the login button an error of invalid username or password will show up.
<?php
session_start();
if(isset($_POST['submit'])) {
include_once("php/config/database.php");
$Email = strip_tags($_POST['Email']);
$Password = strip_tags($_POST['Password']);
$Email = stripslashes($Email);
$Password = stripslashes($Password);
$Email = mysqli_real_escape_string($conn, $Email);
$Password = mysqli_real_escape_string($conn, $Password);
$Password = md5($Password);
$sql = "SELECT * FROM Users WHERE Email='$Email' LIMIT 1";
$query = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($query);
$UserID = $row['UserID'];
$conn_Password = $row['Password'];
if($Password == $conn_Password) {
$_SESSION['Email'] = $Email;
$_SESSION['UserID'] = $UserID;
header("Location: account.php");
} else {
echo "You didn't enter the correct details!";
}
}
?>
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<title>background</title>
<link rel="stylesheet" href="css/style.css">
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="js/index.js"></script>
</head>
<body>
<div class="container demo-1">
<div class="content">
<div id="large-header" class="large-header">
<form action="index.php" method="POST" />
<div class="box">
<h1 id="logintoregister">Login</h1>
<div class="group show">
<input class="inputMaterial" type="text" name="FirstName" >
<span class="highlight"></span>
<span class="bar"></span>
<label>First Name</label>
</div>
<div class="group show">
<input class="inputMaterial" type="text" name="Surname" >
<span class="highlight"></span>
<span class="bar"></span>
<label>Surname</label>
</div>
<div class="group ">
<input class="inputMaterial" type="email" name="Email" >
<span class="highlight"></span>
<span class="bar"></span>
<label>Email</label>
</div>
<div class="group">
<input class="inputMaterial" type="password" id="password" name="Password" >
<span class="highlight"></span>
<span class="bar"></span>
<label>Password</label>
</div>
<div class="group show">
<input class="inputMaterial" type="password" id="confirm_password" >
<span class="highlight"></span>
<span class="bar"></span>
<label>Confirm Password</label>
</div>
<button id="buttonlogintoregister" type = "submit" name="submit">Login</button>
<p id="plogintoregister">By registering, You accept all terms and conditons </p>
<p id="textchange" onclick="register()"> Sign Up</p>
</form>
<!-- Related demos -->
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="js/index.js"></script>
</body>
</html>
> JS code
var cont = 0;
function register(){
cont++;
if(cont==1){
$('.box').animate({height:'600px'}, 550);
$('.show').css('display','block');
$('#logintoregister').text('Register');
$('#buttonlogintoregister').text('Register');
$('#plogintoregister').text("Sei gia' registrato?");
$('#textchange').text('Login');
}
else
{
$('.show').css('display','none');
$('.box').animate({height:'365px'}, 550);
$('#logintoregister').text('Login');
$('#buttonlogintoregister').text('Login');
$('#plogintoregister').text("Non sei iscritto?");
$('#textchange').text('Register');
cont = 0;
}
}
var password = document.getElementById("password")
, confirm_password = document.getElementById("confirm_password");
function validatePassword(){
if(password.value != confirm_password.value) {
confirm_password.setCustomValidity("Passwords Don't Match");
} else {
confirm_password.setCustomValidity('');
}
}
password.onchange = validatePassword;
confirm_password.onkeyup = validatePassword;
I am new to web development and want to make a simple login form, although Not sure why my login button is not responding, the user is required to enter their email ans password. once the user enters a password the login button does not work.
<?php
session_start();
if(isset($_POST['submit'])) {
include_once("php/config/database.php");
$Email = strip_tags($_POST['Email']);
$dbPassword = strip_tags($_POST['Password']);
$Email = stripslashes($Email);
$dbPassword = stripslashes($dbPassword);
$Email = mysqli_real_escape_string($conn, $Email);
$dbPassword = mysqli_real_escape_string($conn, $dbPassword);
$dbPassword = md5($dbPassword);
$sql = ("SELECT * FROM 'Users' WHERE Email='$Email' LIMIT 1");
$query = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($query);
$UserID = $row['UserID'];
$dbpass_Password = $row['Password'];
if($dbPassword == $dbpass_Password) {
$_SESSION['Email'] = $Email;
$_SESSION['UserID'] = $UserID;
header("Location: account.php");
}
else
{
echo "You didn't enter the correct details!";
}
}
?>
<html >
<head>
<meta charset="UTF-8">
<title>background</title>
<link rel="stylesheet" href="css/style.css">
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="js/index.js"></script>
</head>
<body>
<div class="container demo-1">
<div class="content">
<div id="large-header" class="large-header">
<form action="index.php" method="POST>
<div class="box">
<h1 id="logintoregister">Login</h1>
<div class="group show">
<input class="inputMaterial" type="text" name="FirstName" >
<span class="highlight"></span>
<span class="bar"></span>
<label>First Name</label>
</div>
<div class="group show">
<input class="inputMaterial" type="text" name="Surname" >
<span class="highlight"></span>
<span class="bar"></span>
<label>Surname</label>
</div>
<div class="group ">
<input class="inputMaterial" type="email" name="Email" >
<span class="highlight"></span>
<span class="bar"></span>
<label>Email</label>
</div>
<div class="group">
<input class="inputMaterial" type="password" id="password" name="Password" >
<span class="highlight"></span>
<span class="bar"></span>
<label>Password</label>
</div>
<div class="group show">
<input class="inputMaterial" type="password" id="confirm_password" >
<span class="highlight"></span>
<span class="bar"></span>
<label>Confirm Password</label>
</div>
<button id="buttonlogintoregister" type = "submit" name="submit">Login</button>
<p id="plogintoregister">By registering, You accept all terms and conditons </p>
<p id="textchange" onclick="register()"> Sign Up</p>
</form>
<!-- Related demos -->
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src="js/index.js"></script>
</body>
</html>
Your html is broken:
<form action="index.php" method="POST" />
^---
You self-closed the form tag, so EVERY input field on your page is OUTSIDE of your form.
Plus note you have other logic bugs as well:
if($Password == $dbpass_Password) {
^----
$password is an sql-escaped version of the user's PW. If that password naturally contains SQL metacharacters, the password will NOT compare as equal to what comes out of the database. Those escapes are stripped during the DB insert process, and you'll end up doing something like
"Miles O\'Brien" == "Miles O'Brien"
and return a false mismatch.