Validation does not displayed [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Help. Im trying to do validation for the registration code with PHP but it doesn't displayed.
here is my code for the registration form (register.php):
here is the validation, I write it before the
<?php
$name_error = "";
$pass_error = "";
$email_error = "";
$pass_error = "";
$pass2_error = "";
//validation
if(isset($_POST['register'])) {
if(empty($_POST["first-name"] || $_POST["last-name"])) {
$name_error = "Please enter your name.";
}
if(empty($_POST['email'])) {
$email_error = "Please enter the email";
}
if(empty($_POST['password'])) {
$pass_error = "Please enter your password";
}
if(empty($_POST['password2'])) {
$pass2_error = "Please enter to confirm your password";
}
if( $_POST['password2'] != $_POST['password']){
$pass2_error = "The confirm password are incorrect!";
}
else{
header("location:registered.php");
}
}
?>
and here is the form code:
<main>
<div class="register-header d-flex flex-column align-items-center py-5">
<h1 class="font-rale text-dark gray-bg">
Sign up
</h1>
</div>
<form method="post" class="d-flex flex-column align-items-center py-5">
<div class="my-2">
<input type="text" class="name-input mx-1 p-2 border rounded" name="first-name"
placeholder="First name">
<input type="text" class="name-input mx-1 p-2 border rounded" name="last-name" placeholder="Last name">
</div>
<p class="text-center py-2 error"><?php echo $name_error;?></p>
<div class="my-2 p-1">
<input type="email" class="p-2 border rounded" name="email" placeholder="Your email">
</div>
<p class="text-center py-2 error"><?php echo $email_error;?></p>
<div class="my-2 p-1">
<input type="password" class="p-2 border rounded" name="password" placeholder="Your password">
</div>
<p class="text-center py-2 error"><?php echo $pass_error;?></p>
<div class="my-2 p-1">
<input type="password2" class="p-2 border rounded" placeholder="Confirm password">
</div>
<p class="text-center py-2 error"><?php echo $pass2_error;?></p>
<div class="my-2 p-1">
<input type="text" class="p-2 border rounded" name="contact" placeholder="Phone number (Optional)">
</div>
<button type="submit" name="register" class="my-3 px-3 py-2 text-light rounded border-0 form-button">Register</button>
<p>One of us? Sign in here.</p>
</form>
</main>
I have set all the errors to display under the div of each input.
and when I try to leave all input to be empty to test the validation. it doesn't work at all and I just head to the registered.php. Why the validation isn't displayed? Please help..

You must add some flag like:
if(isset($_POST['register'])) {
$flag_names = $flag_email = $flag_password = $flag_password2 = $flag_passwordmatch = true;
if(empty($_POST["first-name"] || $_POST["last-name"])) {
$name_error = "Please enter your name.";
$flag_names = false;
}
if(empty($_POST['email'])) {
$email_error = "Please enter the email";
$flag_email = false;
}
if(empty($_POST['password'])) {
$pass_error = "Please enter your password";
$flag_password = false;
}
if(empty($_POST['password2'])) {
$pass2_error = "Please enter to confirm your password";
$flag_password2 = false;
}
if( $_POST['password2'] !== $_POST['password']){
$pass2_error = "The confirm password are incorrect!";
$flag_passwordmatch = false;
}
if($flag_names && $flag_email && $flag_password && $flag_password2 && $flag_passwordmatch){
//Well I suppose if everything its ok then redirects to this page.
header("location:registered.php");
}
}

Related

How to make PHP register errors go underneath their dedicated input boxes

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>

page doesn't display that text has been sent successfully when clicked on send button

so I'm trying to make the contact form using PHP with validations. So when the input fields are not filled and I click send button it displays the error that says to fill in all blanks but when I fill in everything and click on send button, it sends message to my email address but doesn't display that the message was sent successfully, it just says "page not found" here's my code for contact.php and send.php
<div class="container">
<div class="row text-center">
<div class="col-12 heading">
<h1>Contact Us</h1>
</div>
<div class="col-12">
<?php
$Msg = "";
if(isset($_GET['error'])) {
$Msg = "Please Fill in All Blannks";
echo '<div class="alert alert-danger">'.$Msg.'</div>';
}
if(isset($_GET['success'])) {
$Msg = "Your Message Has Been Sent";
echo '<div class="alert alert-success">'.$Msg.'</div>';
}
?>
</div>
</div>
</div>
<div class="container">
<div class="row text-center">
<div class="col-12">
<form class="reportform" action="send.php" method="post">
<input type="text" name="UName" placeholder="Full Name"><br>
<input type="email" name="Email" placeholder="Your e-mail"><br>
<input type="text" name="Subject" placeholder="Subject"><br>
<textarea name="msg" placeholder="Message"></textarea><br>
<h6>By clicking send button, you agree our <a target="_blank" href="../credits/ppolicy.html">Privacy Policy</a></h6>
<button type="submit" name="btn-send" class="btn btn-primary sendbtn">Send</button>
</form>
</div>
</div>
</div>
<?php
if(isset($_POST['btn-send'])){
$UserName = $_POST['UName'];
$Email = $_POST['Email'];
$Subject = $_POST['Subject'];
$Msg = $_POST['msg'];
if(empty($UserName) || empty($Email) || empty($Subject) || empty($Msg)) {
header('location: contact.php?error');
}
else {
$to = "nika.makhatadze17#gmail.com";
if(mail($to,$Subject,$Msg,$Email)) {
header("location: index.php?success");
}
}
}
else {
header("location: contact.php");
}
?>
By Success you redirect to index.php?success but you defined the Success case in your contact.php, so i would say you just have to change header("location: index.php?success"); to header("location: contact.php?success");
Hey try this code we mead some changes on your code
pass success msg value on header location
<div class="container">
<div class="row text-center">
<div class="col-12 heading">
<h1>Contact Us</h1>
</div>
<div class="col-12">
<?php
$Msg = "";
if(isset($_GET['error'])) {
$Msg = "Please Fill in All Blannks";
echo '<div class="alert alert-danger">'.$Msg.'</div>';
}
if(isset($_GET['success'])) {
$Msg = "Your Message Has Been Sent";
echo '<div class="alert alert-success">'.$Msg.'</div>';
}
?>
</div>
</div>
</div>
<div class="container">
<div class="row text-center">
<div class="col-12">
<form class="reportform" action="send.php" method="post">
<input type="text" name="UName" placeholder="Full Name"><br>
<input type="email" name="Email" placeholder="Your e-mail"><br>
<input type="text" name="Subject" placeholder="Subject"><br>
<textarea name="msg" placeholder="Message"></textarea><br>
<h6>By clicking send button, you agree our <a target="_blank" href="../credits/ppolicy.html">Privacy Policy</a></h6>
<button type="submit" name="btn-send" class="btn btn-primary sendbtn">Send</button>
</form>
</div>
</div>
</div>
<?php
if(isset($_POST['btn-send'])){
$UserName = $_POST['UName'];
$Email = $_POST['Email'];
$Subject = $_POST['Subject'];
$Msg = $_POST['msg'];
$successMsg = 1;
if(empty($UserName) || empty($Email) || empty($Subject) || empty($Msg)) {
header('location: contact.php?error');
}
else {
$to = "nika.makhatadze17#gmail.com";
if(mail($to,$Subject,$Msg,$Email)) {
header("location: index.php?success=".$successMsg);
}
}
}
else {
header("location: contact.php");
}

Contact form not working in wordpress

I am trying to create a contact form in php. But the problem I am facing is that when ever I try to submit the form it shows 404 exception rather than the actual file which has the contact form. The theme is custom made. I have added my code below
<?php
if(isset($_POST)) {
if(trim($_POST["name"]) === "") {
$nameError = "Please enter your name.";
$hasError = true;
} else {
$name = trim($_POST["name"]);
}
if(trim($_POST["email"]) === "") {
$emailError = "Please enter your email address.";
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST["email"]))) {
$emailError = "You entered an invalid email address.";
$hasError = true;
} else {
$email = trim($_POST["email"]);
}
if(trim($_POST["phone"]) === "") {
$phoneError = "Please enter your phone address.";
$hasError = true;
} else {
$phone = trim($_POST["phone"]);
}
if(trim($_POST["comments"]) ==="") {
$commentError = "Please enter a message.";
$hasError = true;
} else {
if(function_exists("stripslashes")) {
$comments = stripslashes(trim($_POST["comments"]));
} else {
$comments = trim($_POST["comments"]);
}
}
if(!isset($hasError)) {
$emailTo = get_option("tz_email");
if (!isset($emailTo) || ($emailTo == "") ){
$emailTo = get_option("admin_email");
}
$subject = "[PHP Snippets] From ".$name;
$body = "Name: $name \n\nEmail: $email \n\nPhone:$phone\n\nComments: $comments";
$headers = "From: ".$name." <".$emailTo.">" . "\r\n" . "Reply-To: " . $email;
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<?php get_header(); ?>
<div class="row" style="margin-top:150px;">
<div class="bg-container"></div>
<div class="col-md-4">
<div class="card contact-form-card">
<form action="<?php esc_url(the_permalink());?>" method="post">
<fieldset>
<legend>Give us your details</legend>
<div class="form-group label-floating">
<label class="control-label" for="user-name">Enter your full name</label>
<input class="form-control" id="user-name" name="name" type="text">
</div>
<div class="form-group label-floating">
<label class="control-label" for="user-email">Enter your email id</label>
<input class="form-control" id="user-email" name="email" type="text">
</div>
<div class="form-group label-floating">
<label class="control-label" for="user-phone">Enter your phone number</label>
<input class="form-control" id="user-phone" name="phone" type="text">
</div>
<div class="form-group label-floating">
<label class="control-label" for="commenets">Comments</label>
<textarea class="form-control" id="commenets" name="comments" type="text"></textarea>
</div>
<button type="submit" name="submit" class="btn btn-primary btn-raised col-xs-12">Submit</button>
</fieldset>
</form>
</div>
</div>
<div class="col-md-8">
<h1 class="text-center">We are currently maintaining over 1000 properties in north Bangalore</h1>
<div class="owl-carousel">
<div class="item card">
<p class="statement">"Poorna is an immaculate real estate consulting professional, and possess several unique skills up his sleeves like property consulting report writing that are rare to find in a professional framework."</p>
<p class="by">Shariq Saleem, Director, Armchair Solutions India</p>
</div>
<div class="item card">
<p class="statement">"Square Capital Assets understands your needs clearly and they are very easy to work with. Once they taken over managing my property, it was a smooth ride"</p>
<p class="by">Manoj</p>
</div>
</div>
</div>
</div>
<div class="row" style="margin-top:15px;">
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/sign_the_agreement.png";?> alt="Sign the agreement">
<h5>Sign with us</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/choose_your_tenants.png";?> alt="Choose your tenants">
<h5>We will find reliable tenants.</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/collect_security_deposit.png";?> alt="Collect Security Deposit">
<h5>Rental Agreement</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/rent_assured.png";?> alt="Rent Assured">
<h5>Collecting rent and keeping your home safe</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/repairs_under_2000.png";?> alt="Repairs Under Rs.2,000">
<h5>Regular check up and maintenance</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/property_tax.png";?> alt="Property Taxes?">
<h5>Managing taxes and bills</h5>
</div>
</div>
</div>
<?php get_footer(); ?>
Just change the fields name like below
<input class="form-control" id="user-name" name="Fname" type="text">
<input class="form-control" id="user-email" name="Femail" type="text">
You will not get 404 error
:)
Naming a field in the form "name" caused that problem.
I renamed it to customer name and it started working

Logical Error with PHP! A password length check

I am getting a logical error I believe I am using PHP as my server side language and I am performing password checks. Users will not be allowed to enter a password less than 8 characters and no more than 32 characters.
register.php
<?php $pageTitle = "Register"; ?>
<?php $sectoin = "signing"; ?>
<?php include 'INC/header.php'; ?>
<?php
$submit = $_POST['submit'];
$username = strip_tags($_POST['username']);
$password = strip_tags($_POST['password']);
$repeatPassword = strip_tags($_POST['repeatPassword']);
$email = strip_tags($_POST['email']);
$date = date("m-d-Y");
if ($submit) {
// Checking for exsistence
if ($username && $password && repeatPassword && $email) {
// Encrypts the Pasword
$password = md5($password);
$repeatPassword = md5($repeatPassword);
// Do Passwords Match
if ($password == $repeatPassword) {
// Check Character Length of Username
if (strlen($username) > 16 || strlen($username) <= 2) {
echo "<h3 class='text-center'> <span class='alert alert-warning'> Your <b>Username</b> must be between 3 and 16 characters! </h3> </span>";
} else {
// Check Password Length
if (strlen($password && $repeatPassword) < 8) {
echo "<h3 class='text-center'> <span class='alert alert-warning'> Your <b>Password</b> is less than 8 characters! </h3> </span>";
} else {
echo 'Registration Completed!';
}
}
} else echo "<h3 class='text-center'> <span class='alert alert-danger'> Your <b>Passwords</b> must match! </h3> </span>";
} else echo "<h3 class='text-center'> <span class='alert alert-warning'> Please fill out <b>All</b> fields!</h3> </span>";
}
?>
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<div class="page-header">
<h3 class="text-center"> Registration </h3>
</div>
<form class="form-horizontal" role="form" action="register.php" method="POST">
<!--Start of Username-->
<div class="form-group">
<label for="username" class="col-sm-2 control-label"> Username </label>
<div class="col-sm-10">
<div class="input-group">
<span class="input-group-addon"> <span class="glyphicon glyphicon-user"> </span> </span>
<input type="text" name="username" class="form-control" id="username" placeholder="Username" />
</div>
</div>
</div>
<!--End of Username-->
<!--Start of E-Mail-->
<div class="form-group">
<label for="email" class="col-sm-2 control-label"> E-Mail </label>
<div class="col-sm-10">
<div class="input-group">
<span class="input-group-addon"> <span class="glyphicon glyphicon-envelope"> </span> </span>
<input type="email" name="email" class="form-control" id="email" placeholder="E-Mail" />
</div>
</div>
</div>
<!--End of E-Mail-->
<!--Start of Password-->
<div class="form-group">
<label for="password" class="col-sm-2 control-label"> Password </label>
<div class="col-sm-10">
<div class="input-group">
<span class="input-group-addon"> <span class="glyphicon glyphicon-star"> </span> </span>
<input type="password" name="password" class="form-control" id="password" placeholder="Password" />
</div>
</div>
</div>
<!--End of Password-->
<!--Start of Repeat Password-->
<div class="form-group">
<label for="repeatPassword" class="col-sm-2 control-label"> <span id="repeatPassword"> Repeat Password </span> </label>
<div class="col-sm-10">
<div class="input-group">
<span class="input-group-addon"> <span class="glyphicon glyphicon-check"> </span> </span>
<input type="password" name="repeatPassword" class="form-control" id="password" placeholder="Password" />
</div>
</div>
<!--End of Repeat Password-->
<!--Start of Checkbox and Submit Button-->
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<label class="checkbox">
<input type="checkbox" name="rememberMe" value="rememberMe" id="rememberMe"> <span id="rememberUs"> Remember Me </span>
</label>
<button type="submit" name="submit" value="Register" class="btn btn-primary slideToTheLeft"> Register </button>
</div>
</div>
</form>
</div>
</div>
</div>
<div class="col-sm-2"></div>
</div>
<?php include 'INC/footer.php'; ?>
If I take the '&& $repeatPassword' out of the equation it skips the condition and echos.
if (strlen($password && $repeatPassword) < 8 {
However, it is called that $repeatPassword = $password, so I shouldn't even need the '&& repeatPassword' but it won't do anything if it is included into the code.
However, the main problem besides that is that no matter what the if statement is stating that no matter what the password is less than 8 characters.
As a means of testing if supplied values are within permitted bounds you could try:
$valid_username=in_array( strlen( $username ), range(2,16) );
$valid_password=$password===$repeatPassword && in_array( strlen( $password ), range(8,32) );
if( $valid_username && $valid_password ){/* all good */}
example:
if( $submit ) {
if( $username && $password && $repeatPassword && $email ) {
// hash the Passwords
$password = md5( trim( $password ) );
$repeatPassword = md5( trim( $repeatPassword ) );
$unrange=range(3,16);
$pwdrange=range(8,32);
$valid = true;
$valid_username = in_array( strlen( $username ), $unrange );
$valid_password = in_array( strlen( $password ), $pwdrange );
$password_match = $password===$repeatPassword;
if( !$valid_password ){
$valid=false;
echo "<h3 class='text-center'> <span class='alert alert-warning'> Your <b>Password</b> should be between ".min( $pwdrange )." and ".max( $pwdrange )." characters! </h3> </span>";
}
if( !$valid_username ){
$valid=false;
echo "<h3 class='text-center'> <span class='alert alert-warning'> Your <b>Username</b> must be between ".min( $unrange )." and ".max( $unrange )." characters! </h3> </span>";
}
if( !$password_match ){
$valid=false;
echo "<h3 class='text-center'> <span class='alert alert-danger'> Your <b>Passwords</b> must match! </h3> </span>";
}
if( $valid ) {
echo 'Registration Completed!';
/* add to db? */
}
} else echo "<h3 class='text-center'> <span class='alert alert-warning'> Please fill out <b>All</b> fields!</h3> </span>";
}
You probably don't need to check the length of both $password and $repeatPassword since you will also be checking to see if they match each other.
if (strlen($password < 8) {
// error
} elseif ($password != $repeatPassword) {
// error
} else {
// ALL IS GOOD !
}
I had actually solved my problem, I decided to take out the or to the password and it had solved the problem and everything is up and running.
Try this:
if (strlen($password) >= 8 && strlen($password) <= 32) {
if (strlen($repeatpassword) >= 8 && strlen($repeatpassword) <= 32) {
// code here
}
}
I think it's a bit odd to check both passwords, however.
So, my solution would be:
if (strlen($password) >= 8 && strlen($password) <= 32) {
// code here
}

Disable button after submit PHP

I have this code that when submitted, it generates random number from the array.
<?php
if(isset($_POST['roll'])) {
$randarray = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
$randselect = array_rand($randarray);
$nr = $randarray[$randselect];
echo '<p class="btn btn-info"> Branch: '. $nr. '</p>';
}
?>
<form action="#" method="post">
<button type="submit" class="btn btn-default" name="roll">Roll Branch </button>
</form>
What I wanted to do is after the form was submitted, the button will be disabled. Any thoughts?
Just add disabled if the button is pressed, e.g.
<button type="submit" class="btn btn-default" name="roll" <?php echo isset($_POST["roll"]) ? "disabled" : "";?>>Roll Branch </button>
You can do this:
<button type="submit" class="btn btn-default" name="roll" <?php echo isset($_POST['roll']) ? 'disabled="true"' : ''; ?> >Roll Branch </button>
Add onclick="this.disabled='true';" for button tag
It is not getting enabled after page post and I got some error.
In my situation my signup page have a submit button. Below is the code :
<form action="" method="post" id="signup-form" name="signup-form">
<div class="row">
<div class="form-group col-md-12 mb-4">
<input type="text" class="form-control input-lg" id="txtFormNo" name="txtFormNo" aria-describedby="nameHelp" placeholder="Form No" required readonly>
</div>
<div class="form-group col-md-12 mb-4">
<label for="standard">Select Standard</label>
<select class="form-control" id="standard" name="standard" required>
<option>FYJC</option>
<option>SYJC</option>
</select>
</div>
<div class="col-sm-12">
<div class="row">
<div class="col-6">
<div class="form-group">
<label for="txtSeatNo">SSC Seat No</label>
<input type="text" class="form-control input-lg" id="txtSeatNo" name="txtSeatNo" autocomplete="off" aria-describedby="nameHelp" placeholder="SSC Seat No" maxlength="7" required>
</div>
</div>
<div class="col-6">
<div class="form-group">
<label for="confirmSeatNo">Re-enter SSC Seat No</label>
<input type="text" class="form-control input-lg" id="confirmSeatNo" name="confirmSeatNo" autocomplete="off" aria-describedby="nameHelp" placeholder="Re-enter SSC Seat No" maxlength="7" required>
</div>
</div>
</div>
</div>
<?php if (isset($_GET['flag']) != 'newsyjc') { ?>
<div class="col-sm-12">
<div class="row">
<div class="col-6">
<div class="form-group">
<label for="txtSscAppNo">Enter Unique Application ID</label>
<input type="text" class="form-control input-lg" placeholder="UNIQUE APPLICATION ID" autocomplete="off" name="txtSscAppNo" id="txtSscAppNo" maxlength="6" required />
</div>
</div>
<div class="col-6">
<div class="form-group">
<label for="confirmSscAppNo">Re-enter Unique Application ID</label>
<input type="text" class="form-control input-lg" placeholder="Re-enter UNIQUE APPLICATION ID" autocomplete="off" name="confirmSscAppNo" id="confirmSscAppNo" maxlength="6" required />
</div>
</div>
</div>
</div>
<?php } ?>
<div class="col-sm-12">
<div class="row">
<div class="col-6">
<div class="form-group">
<label for="txtContact">Enter Contact No.</label>
<input type="text" class="form-control input-lg" id="txtContact" name="txtContact" autocomplete="off" placeholder="Contact" maxlength="10" required>
</div>
</div>
<div class="col-6">
<div class="form-group">
<label for="confirmContact">Re-enter Contact No.</label>
<input type="text" class="form-control input-lg" id="confirmContact" name="confirmContact" autocomplete="off" placeholder="Re-enter Contact" maxlength="10" required>
</div>
</div>
</div>
</div>
<div class="col-sm-12">
<div class="row">
<div class="col-6">
<div class="form-group">
<label for="txtEmail">Enter Email ID</label>
<input type="email" class="form-control input-lg" id="txtEmail" name="txtEmail" aria-describedby="emailHelp" autocomplete="off" placeholder="Email ID" required>
</div>
</div>
<div class="col-6">
<div class="form-group">
<label for="confirmEmail">Re-enter Email ID</label>
<input type="email" class="form-control input-lg" id="confirmEmail" name="confirmEmail" aria-describedby="emailHelp" autocomplete="off" placeholder="Re-enter Email ID" required>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<div class="d-inline-block mr-3">
<label class="control control-checkbox">
<input type="checkbox" required />
<div class="control-indicator"></div>
I Agree the terms and conditions
</label>
</div>
<button type="submit" class="btn btn-lg btn-primary btn-block mb-4" name = "btnSignup" id = "btnSignup"
<?php echo isset($_POST["btnSignup"]) ? "disabled" : "";?> >Sign Up</button>
<p>Already have an account?
<a class="text-blue" href="login.php">Sign in</a>
</p>
</div>
</div>
</form>
PHP Code :
if (isset($_POST['btnSignup'])) {
$standard = trim($_POST['standard']);
$SeatNo = trim($_POST['txtSeatNo']);
$cSeatNo = trim($_POST['confirmSeatNo']);
$FormNo = trim($_POST['txtFormNo']);
$contact = trim($_POST['txtContact']);
$cContact = trim($_POST['confirmContact']);
$Email = trim($_POST['txtEmail']);
$cEmail = trim($_POST['confirmEmail']);
$code = md5(uniqid(rand()));
$stmt = $user->runQuery("SELECT * FROM stud_personal_details WHERE SeatNo=:SeatNo");
$stmt->execute(array(":SeatNo"=>$SeatNo));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if ($flag == 'newsyjc') {
$SscAppNo = '';
$cSscAppNo = '';
if ($stmt->rowCount() > 0) {
$msg = "<div class='alert alert-error'>
<strong>Sorry !</strong> Seat No is Already Registered.<br>
Please enter your correct Seat No OR <a class='btn btn-success btn-lg' href='login.php'>Click here to login !</a>
</div>";
} else if ($standard == 'SYJC') {
if ($user->stud_personal_details($SeatNo, $FormNo, $SscAppNo, $Password, $contact, $code, $Email) &&
$user->stud_admission_details($SeatNo, $SscAppNo) &&
$user->stu_app($SeatNo, $code) &&
$user->Marks($SeatNo) ) {
$user->send_sms($SeatNo, $Password, $Email);
$msg = "<div class='alert alert-success' role='alert'>
<strong>Success!</strong> Your account is created.<br>
Password have been SMS on ".$contact." and mailed at ".$Email."<br>
<p><a class='text-blue' href='login.php'>Click here to login !</a></p>
</div>";
//header("refresh:3;login.php?flag=".$flag);
} else {
$msg = "<div class='alert alert-danger' role='alert'>
<strong>Sorry !</strong> Something went wrong.<br>
Contact college office or IT Charge of College.
</div>";
}
}
} else if ($days > 0) {
$SscAppNo = trim($_POST['txtSscAppNo']);
$cSscAppNo = trim($_POST['confirmSscAppNo']);
if (strlen($SeatNo) != 7 ) {
$msg = "<div class='alert alert-danger' role='alert'>
<i class='mdi mdi-alert mr-1'></i> SSC Seat Number is a 7 alpha-numberic combination.<br>
Please enter again.
</div>";
} else if ($cSeatNo !== $SeatNo) {
$msg = "<div class='alert alert-danger' role='alert'>
<i class='mdi mdi-alert mr-1'></i> <strong>Sorry!</strong> SSC Seat Number doesn't match.<br>
Please enter again.
</div>";
} else if (strlen($SscAppNo) != 6 ) {
$msg = "<div class='alert alert-danger' role='alert'>
<i class='mdi mdi-alert mr-1'></i> UNIQUE APPLICATION ID should be 6 numberic value.<br>
Please enter again.
</div>";
} else if ($cSscAppNo !== $SscAppNo) {
$msg = "<div class='alert alert-danger' role='alert'>
<i class='mdi mdi-alert mr-1'></i> <strong>Sorry!</strong> UNIQUE APPLICATION ID doesn't match.<br>
Please enter again.
</div>";
} else if (strlen($contact) != 10 ) {
$msg = "<div class='alert alert-danger' role='alert'>
<i class='mdi mdi-alert mr-1'></i> Contact No. should be of 10 digits.
</div>";
} else if ($contact!==$cContact) {
$msg = "<div class='alert alert-danger' role='alert'>
<i class='mdi mdi-alert mr-1'></i> <strong>Sorry!</strong> Contact No. Doesn't match.
</div>";
} else if ($cEmail!==$Email) {
$msg = "<div class='alert alert-danger' role='alert'>
<i class='mdi mdi-alert mr-1'></i> <strong>Sorry!</strong> Email Doesn't match.<br>
Please enter again.
</div>";
} else if ($stmt->rowCount() > 0) {
$msg = "<div class='alert alert-danger' role='alert'>
<i class='mdi mdi-alert mr-1'></i> <strong>Sorry !</strong> Seat No is Already Registered.<br>
Please enter your correct Seat No OR <a class='btn btn-success btn-lg' href='login.php?days=".$days."'>Click here to login !</a>
</div>";
} else if ($standard == 'FYJC') {
if ($user->stud_personal_details($SeatNo, $FormNo, $SscAppNo, $Password, $contact, $code, $Email) &&
$user->stud_admission_details($SeatNo, $SscAppNo) &&
$user->stu_app($SeatNo, $code) &&
$user->Marks($SeatNo) ) {
$user->send_sms($SeatNo, $Password, $Email, $contact);
$msg = "<div class='alert alert-success' role='alert'>
<strong>Success!</strong> Your account is created.<br>
Password have been send on ".$contact." and also mailed at ".$Email."<br>
<a class='btn btn-success btn-lg' href='login.php?days=".$days."'>Click here to login !</a>
</div>";
} else {
$msg = "<div class='alert alert-danger' role='alert'>
<strong>Sorry !</strong> Something went wrong...<br>
Contact college office or IT Charge of College.
</div>";
}
}
}
}
It stays disabled incase of some error or otherwise even after success. I mean to say it should stay disable when clicked but should get enable as soon as some error or success occurs so that user can again fill the form and click on submit again.

Categories