Instead of a div being shown, the form is sent - php

The following should output a div if the data is not entered in the input fields or if the passwords don't match, but it does not happen:
<?php
$data = $_POST;
if(isset($data['action_signup'])){
$errors = array();
if(trim($data['email'])==''){
$errors[] = 'Введите email';
}
if(trim($data['login'])==''){
$errors[] = 'Введите имя пользователя';
}
if($data['password']==''){
$errors[] = 'Введите пароль';
}
if($data['enterpassword'] != $data['password']){
$errors[] = 'Пароль введен не верно';
}
if(empty($errors)){
//Все заебись
}else{
echo '<div class="error_div">'.array_shift($errors).'</div>';
}
}
?>
Form:
<form action="/Register.php" method="post">
<div class="containerForTextRegister"><a class="register">РЕГИСТРАЦИЯ</a>
</div>
<div class="container_inputs">
<input class="register_input_email" name="email" placeholder="e-mail" required type="email" maxlength="40" value="<?php echo #$data['email'];?>">
<input class="register_input_login" name="login" placeholder="login" required maxlength="12" value="<?php echo #$data['login'];?>">
<input class="register_input_password" name="password" placeholder="password" required pattern="^[a-zA-Z]+$" maxlength="30">
<input class="register_input_enterpassword" name="enterpassword" placeholder="enter password" required pattern="^[a-zA-Z]+$" maxlength="30">
<div class="buttons_container">
<button class="button_entrance"><a class="text_button_entrance">войти</a></button>
<button class="button_register"><a class="text_button_register" name="action_signup">регистрация</a></button>
</div>
</div>
</form>
A div window should appear, but the data is just sent and that’s it. Help, I will be very grateful

You have to change your html code <button class="button_register"><a class="text_button_register" name="action_signup">регистрация</a></button> to
Or
if(isset($data['action_signup'])) to if(isset($data['email']))
like
<?php
$data = $_POST;
if(isset($data['email'])){
$errors = array();
if(trim($data['email'])==''){
$errors[] = 'Введите email';
}
if(trim($data['login'])==''){
$errors[] = 'Введите имя пользователя';
}
if($data['password']==''){
$errors[] = 'Введите пароль';
}
if($data['enterpassword'] != $data['password']){
$errors[] = 'Пароль введен не верно';
}
if(empty($errors)){
//Все заебись
}else{
echo '<div class="error_div">'.array_shift($errors).'</div>';
}
}
?>

Related

How Can I show array content inside the body of html

This is my custom user registration form WordPress site, actually, this is my first custom development, and here all the data passes the DB my problem is I need to show my error message inside the HTML code. how can I do it? can anyone help me to solve this problem? now my error messages show like this (Array ( [username_empty] => Needed Username [email_valid] => Email has no valid value [texnumber_empty] => Needed Tax Number )) but I need only show error message only Ex: this one ( [username_empty] => Needed Username) I need to show "Needed Username"
Like this.
if (is_user_logged_in()) {
// echo '<script>alert("Welcome, registered user!")</script>';
echo '<script type="text/javascript">';
echo 'alert("Welcome, registered user!");';
echo 'window.location.href = "Url";';
echo '</script>';
} else {
// echo 'Welcome, visitor!';
global $wpdb;
if ($_POST) {
$username = $wpdb->escape($_POST['user_login']);
$email = $wpdb->escape($_POST['user_email']);
$taxnumber = $wpdb->escape($_POST['tax_number']);
$password = $wpdb->escape($_POST['user_pass']);
$ConfPassword = $wpdb->escape($_POST['user_confirm_password']);
$error = array();
if (strpos($username, ' ') !== FALSE) {
$error['username_space'] = "Username has Space";
}
if (empty($username)) {
$error['username_empty'] = "Needed Username";
}
if (username_exists($username)) {
$error['username_exists'] = "Username already exists";
}
if (!is_email($email)) {
$error['email_valid'] = "Email has no valid value";
}
if (email_exists($email)) {
$error['email_existence'] = "Email already exists";
}
if (empty($taxnumber)) {
$error['texnumber_empty'] = "Needed Tax Number";
}
if (strcmp($password, $ConfPassword) !== 0) {
$error['password'] = "Password didn't match";
}
if (count($error) == 0) {
$user_id = wp_create_user($username, $password, $email);
$userinfo = array(
'ID' => $user_id,
'user_login' => $username,
'user_email' => $email,
'user_pass' => $password,
'role' => 'customer',
);
// Update the WordPress User object with first and last name.
wp_update_user($userinfo);
// Add the company as user metadata
update_user_meta($user_id, 'tax_number', $taxnumber);
echo '<script type="text/javascript">';
echo 'alert("User Created Successfully");';
echo 'window.location.href = "url";';
echo '</script>';
exit();
} else {
print_r($error);
}
}
?>
<section id="wholesale-custom-register-form">
<div class="container wholesale-custom-register-form">
<div class="register-form">
<div class="register-form-title">
<h1>Wholesale Register Form</h1>
</div>
<div class="wholesale-register">
<form class="register-fm" method="POST">
<div class="form-group">
<label>User Name</label>
<input class="form-control" type="text" name="user_login" id="user_login" placeholder="Username" />
<?php foreach ($error as $error) {
echo $error . "<br>";
} ?>
</div>
<div class="form-group">
<label>Email</label>
<input class="form-control" type="email" name="user_email" id="user_email" placeholder="Email" />
</div>
<div class="form-group">
<label>Tax Number</label>
<input class="form-control" type="text" name="tax_number" id="tax_number" placeholder="Tax Number" />
</div>
<div class="form-group">
<label>Enter Password</label>
<input class="form-control" type="password" name="user_pass" id="user_pass" placeholder="Password" />
</div>
<div class="form-group">
<label>Enter Cofirm Password</label>
<input class="form-control" type="password" name="user_confirm_password" id="user_confirm_password" placeholder="Cofirm Password" />
</div>
<div class="form-group">
<button class="custom-register-btn" type="submit" name="btnsubmit">Log In</button>
</div>
</form>
</div>
</div>
</div>
</section>
<?php
};
This is my code I will try many times but I can't get the error messages inside the HTML body.
You want to make an AJAX call to register a user then use a callback function to check for success. If a field is invalid you also check it with javascript.
So you would need to refactor your code, seperate it into frontend/backend code and connect it via AJAX.
Write your PHP code as "add_action_hook" and register function
Onclick validate fields and inputs
Call the hook via AJAX (url: "/wp-admin/admin-ajax.php")
Return result
These are just very abstract steps, you'll need to gather some intel for yourself. You could take a look at this: https://awhitepixel.com/blog/wordpress-use-ajax/ and https://docs.wpvip.com/technical-references/security/validating-sanitizing-and-escaping/
I would do something like this
if (is_user_logged_in()) {
// echo '<script>alert("Welcome, registered user!")</script>';
echo '<script type="text/javascript">';
echo 'alert("Welcome, registered user!");';
echo 'window.location.href = "Url";';
echo '</script>';
} else {
// echo 'Welcome, visitor!';
global $wpdb;
if ($_POST) {
$username = $wpdb->escape($_POST['user_login']);
$email = $wpdb->escape($_POST['user_email']);
$taxnumber = $wpdb->escape($_POST['tax_number']);
$password = $wpdb->escape($_POST['user_pass']);
$ConfPassword = $wpdb->escape($_POST['user_confirm_password']);
if (strpos($username, ' ') !== FALSE) {
$errorMsg[] = "Username has Space";
}
if (empty($username)) {
$errorMsg[] = "Needed Username";
}
if (username_exists($username)) {
$errorMsg[] = "Username already exists";
}
if (!is_email($email)) {
$errorMsg[] = "Email has no valid value";
}
if (email_exists($email)) {
$errorMsg[] = "Email already exists";
}
if (empty($taxnumber)) {
$errorMsg[] = "Needed Tax Number";
}
if (strcmp($password, $ConfPassword) !== 0) {
$errorMsg[] = "Password didn't match";
}
if (count($errorMsg) == 0) {
$user_id = wp_create_user($username, $password, $email);
$userinfo = array(
'ID' => $user_id,
'user_login' => $username,
'user_email' => $email,
'user_pass' => $password,
'role' => 'customer',
);
// Update the WordPress User object with first and last name.
wp_update_user($userinfo);
// Add the company as user metadata
update_user_meta($user_id, 'tax_number', $taxnumber);
echo '<script type="text/javascript">';
echo 'alert("User Created Successfully");';
echo 'window.location.href = "url";';
echo '</script>';
exit();
} else {
print_r($errorMsg);
}
}
?>
<section id="wholesale-custom-register-form">
<div class="container wholesale-custom-register-form">
<div class="register-form">
<div class="register-form-title">
<h1>Wholesale Register Form</h1>
</div>
<div class="wholesale-register">
<form class="register-fm" method="POST">
<div class="form-group">
<label>User Name</label>
<input class="form-control" type="text" name="user_login" id="user_login" placeholder="Username" />
<?php foreach ($errorMsg as $error) {
?>
<div>
<strong><?= $error; ?> </strong>
</div>
<?php
} ?>
</div>
<div class="form-group">
<label>Email</label>
<input class="form-control" type="email" name="user_email" id="user_email" placeholder="Email" />
</div>
<div class="form-group">
<label>Tax Number</label>
<input class="form-control" type="text" name="tax_number" id="tax_number" placeholder="Tax Number" />
</div>
<div class="form-group">
<label>Enter Password</label>
<input class="form-control" type="password" name="user_pass" id="user_pass" placeholder="Password" />
</div>
<div class="form-group">
<label>Enter Cofirm Password</label>
<input class="form-control" type="password" name="user_confirm_password" id="user_confirm_password" placeholder="Cofirm Password" />
</div>
<div class="form-group">
<button class="custom-register-btn" type="submit" name="btnsubmit">Log In</button>
</div>
</form>
</div>
</div>
</div>
</section>
<?php
};

How validate forms without js jq

Hello i do have a problem,i do not use js or jq just bootstrap link in head,it shoud pop up if forms are invalid but for some reason it wont,thats my prob.On some my other codes it is working but now it wont here i dont know why,i wouldnt post this,but struggling with trivials like this for a day :( dont give me minuses,i dont ask for plus but neither - I would appreciate hlp!
*Signup back*
<?php
if(isset($_POST['signup-submit'])){
require 'db.inc.php';
$error = false;
$username = $_POST['uid'];
$email = $_POST['mail'];
$pwd = $_POST['pwd'];
$pwdrpt = $_POST['pwd_repeat'];
if (empty($username) || empty($email) || empty($pwd) || empty($pwdrpt)) {
header("location: ../signup.php?error=emptyfields&uid=".$username."&mail=".$email);
exit();
}
// else if (!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA-Z0-9]*$/", $username)){
// header("location: ../signup.php?error=invalidmail&uid");
// exit();
// }
elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)){
$error = true;
$errorEmail = 'Please input a valid Email address';
header("location: ../signup.php?error=invalidmail&uid=".$username);
exit();
}
elseif (!preg_match("/^[a-zA-Z0-9]*$/", $username)){
$error = true;
$errorUsername = 'Please input username';
header("location: ../signup.php?error=invaliduid&mail=".$email);
exit();
}
*SignUp front*
<form action="includes/signup.inc.php" method="post" novalidate>
<h3 align="center"><strong>Sign Up<strong></h3>
<!-- <?php
if (isset($_GET['error'])) {
if ($_GET['error'] == 'invalidmail') {
echo '<p class="signuperror">Enter valid email address!</p>';
}
}
?> -->
<input class="form-control" type="text" placeholder="Username" name="uid">
<div><span class="text-danger"><?php if(isset($errorUsername)) echo $errorUsername; ?></span>
</div>
<br>
<input class="form-control" type="text" placeholder="Enter Email" name="mail">
<div><span class="text-danger"><?php if(isset($errorEmail)) echo $errorEmail; ?></span></div>
Try this
<?php
if(isset($_POST['signup-submit'])){
$error = false;
$username = $_POST['uid'];
$email = $_POST['mail'];
if (empty($username) || empty($email)) {
header("location: index1.php?error=emptyfields");
exit();
}
elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)){
$error = true;
$errorEmail = 'Please input a valid Email address';
header("location: index1.php?error=invalidmail");
exit();
}
elseif (!preg_match("/^[a-zA-Z0-9]*$/", $username)){
$error = true;
$errorUsername = 'Please input username';
header("location: index1.php?error=invaliduid");
exit();
}
}
?>
<form action="" method="post" novalidate>
<h3 align="center"><strong>Sign Up<strong></h3>
<?php
if (isset($_GET['error'])) {
if ($_GET['error'] == 'invalidmail') {
echo '<p class="signuperror">Enter valid email address!</p>';
}
}
?>
<input class="form-control" type="text" placeholder="Username" name="uid">
<div><span class="text-danger"><?php if(isset($errorUsername)) echo $errorUsername; ?></span>
</div>
<br>
<input class="form-control" type="text" placeholder="Enter Email" name="mail">
<div><span class="text-danger"><?php if(isset($errorEmail)) echo $errorEmail; ?></span></div>
<input type="submit" name="signup-submit" id="signup-submit">
</form>

After submit form csv file saved and i want redirect another website

I have a form set up and a php file (as shown below) that I have saved data in csv file to validate the input and then redirect to a different website (index.html). The validation and csv export works perfectly, but I can't figure out how to get the form to redirect to the wanted page instead of just showing the post return.
<?php
//index.php
$error = '';
$name = '';
$email = '';
$phone = '';
$message = '';
function clean_text($string)
{
$string = trim($string);
$string = stripslashes($string);
$string = htmlspecialchars($string);
return $string;
}
if(isset($_POST["submit"]))
{
if(empty($_POST["name"]))
{
$error .= '<p><label class="text-danger">Please Enter your Name</label></p>';
}
else
{
$name = clean_text($_POST["name"]);
if(!preg_match("/^[a-zA-Z ]*$/",$name))
{
$error .= '<p><label class="text-danger">Only letters and white space allowed</label></p>';
}
}
if(empty($_POST["email"]))
{
$error .= '<p><label class="text-danger">Please Enter your Email</label></p>';
}
else
{
$email = clean_text($_POST["email"]);
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$error .= '<p><label class="text-danger">Invalid email format</label></p>';
}
}
if(empty($_POST["phone"]))
{
$error .= '<p><label class="text-danger">phone is required</label></p>';
}
else
{
$phone = clean_text($_POST["phone"]);
}
if(empty($_POST["message"]))
{
$error .= '<p><label class="text-danger">Message is required</label></p>';
}
else
{
$message = clean_text($_POST["message"]);
}
if($error == '')
{
$file_open = fopen("enquiry_form_data.csv", "a");
$no_rows = count(file("enquiry_form_data.csv"));
if($no_rows > 1)
{
$no_rows = ($no_rows - 1) + 1;
}
$form_data = array(
'sr_no' => $no_rows,
'name' => $name,
'email' => $email,
'phone' => $phone,
'message' => $message
);
fputcsv($file_open, $form_data);
$error = '<label class="text-success">Thank you for contacting us</label>';
$name = '';
$email = '';
$phone = '';
$message = '';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>How to Store Form data in CSV File using PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container"> <br />
<div class="col-md-6" style="margin:0 auto; float:none;">
<form method="post">
<h3 align="center">Find your dream Holiday today!</h3>
<br />
<?php echo $error; ?>
<div class="form-group">
<label>Enter Name</label>
<input type="text" name="name" placeholder="Enter Name" class="form-control" value="<?php echo $name; ?>" />
</div>
<div class="form-group">
<label>Enter Email</label>
<input type="text" name="email" class="form-control" placeholder="Enter Email" value="<?php echo $email; ?>" />
</div>
<div class="form-group">
<label>Enter phone</label>
<input type="text" name="phone" class="form-control" placeholder="Enter phone" value="<?php echo $phone; ?>" />
</div>
<div class="form-group">
<label>Enter Message</label>
<textarea name="message" class="form-control" placeholder="Enter Message"><?php echo $message; ?></textarea>
</div>
<div class="form-group" align="center">
<input type="submit" name="submit" class="btn btn-info" value="Submit" />
</div>
</form>
</div>
</div>
</body>
</html>
I have a form set up and a php file (as shown below) that I have saved data in csv file to validate the input and then redirect to a different website (index.html). The validation and csv export works perfectly, but I can't figure out how to get the form to redirect to the wanted page instead of just showing the post return.
Try below code:
<?php
header('Location: http://www.example.com/');
?>

Undefined index in Function and Confirm password is not working

I'm converting my Form Validation code into function but problem is that it's giving error Undefined index of $confirm variable which is already define and also confirm password is not working.
Function
function formValidation($action,$confirm){
$result = "";
$input = $_POST[$action];
$confirm = $_POST[$confirm];
// For Email Validation
$find = 'email';
$path = $action;
$pos = strpos($path,$find);
if(empty(user_input($input))){
$result = "$action is missing";
}elseif($pos !== false){
$email = filter_var($input, FILTER_SANITIZE_EMAIL);
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$result = "Email invalid format";
}
}elseif($confirm !== $_POST['password']){
$result = "password is not confirm";
}
return $result;
}
And there is any way I call function one time and it checkx all fields and return error
Trigger
$email_err = $password_err = $username_err = $confirmPWD_err = "";
if(isset($_POST['submit'])){
$email_err = formValidation('prd_email','');
$password_err = formValidation('password','');
$username_err = formValidation('username','');
$confirmPWD_err = formValidation('password','confirm');
}
HTML
<form method="post">
<div class="form-group">
<input class="form-control" placeholder="username" name="username" type="text" />
<?php echo $username_err ?>
</div>
<div class="form-group">
<input class="form-control" placeholder="email" name="prd_email" type="text" />
<?php echo $email_err ?>
</div>
<div class="form-group">
<input class="form-control" placeholder="password" name="password" type="password" />
<?php echo $password_err ?>
</div>
<div class="form-group">
<input class="form-control" placeholder="Confirm Password" name="confirm" type="password" />
<?php echo $confirmPWD_err ?>
</div>
<input type="submit" class="btn btn-success" name="submit" value="submit" />
</form>
Try this, is it working for you?
function formValidation($action,$confirm = null){
$result = "";
$input = $_POST[$action];
if($confirm){$confirm = $_POST[$confirm];}
// For Email Validation
$find = 'email';
$path = $action;
$pos = strpos($path,$find);
if(empty(user_input($input))){
$result = "$action is missing";
}elseif($pos !== false){
$email = filter_var($input, FILTER_SANITIZE_EMAIL);
if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
$result = "Email invalid format";
}
}elseif(!empty($confirm) && $confirm !== $_POST['password']){
$result = "password is not confirm";
}
return $result;
}
And call the function like this
if (isset($_POST['submit'])) {
$email_err = formValidation('prd_email');
$password_err = formValidation('password');
$username_err = formValidation('username');
$confirmPWD_err = formValidation('password', 'confirm');
}

Forgot password reset not displaying correct email address and not updating password if any error made by user during for submission

I am currently working on PHP forgot password reset, which partially doing the job but seeking some assistance to improve it further.
1st issue: It is not displaying the correct email address on the
submission form. It updates the password correctly but doesn't
display correct email address.
2nd issue: Also if the user makes an error while submitting the form on reloading the page doesn't update the password hence the user has to go back to his email to click back on the link.
<?php
include('../config/connection.php');
if(isset($_POST['submit'])){
$password = mysqli_real_escape_string($dbc,$_POST['password']);
$Rpassword = mysqli_real_escape_string($dbc,$_POST['Rpassword']);
$acode=$_POST['encrypt'];
$passmd = md5(SHA1($password));
if (empty($password) OR empty($Rpassword)) {
$error = 'One or either field is missing';
} if ($password != $Rpassword) {
$error = 'Passwords don\'t match';
} if(strlen($password)<6 OR strlen($Rpassword)>20) {
$error = 'Password must be between 6 to 20 characters';
}
else {
$query = mysqli_query($dbc,"select * from users where passreset='$acode'") or die(mysqli_error($dbc));
if (mysqli_num_rows ($query)==1)
{
$query3 = mysqli_query($dbc,"UPDATE users SET password='$passmd',passreset=0 WHERE passreset='$acode'")
or die(mysqli_error($dbc));
$sent = 'Password has been Changed successfully, Please sign in for loging in.';
}
else
{
$error = 'Please click back on the Forgot password link to reset your password ';
}
}
}
?>
<body>
<?php if(!isset($_POST['submit']) OR $error != '' OR isset($error)) { ?>
<?php if(isset($error) AND $error !='')
{
echo '<p style="color:#c43235">'.$error.'</p>';
}
?>
<form action="reset.php" method="post" role="form">
<div class="form-group">
<label for="password">Email</label>
<input type="text" class="form-control" id="email" name="email" value="
<?php
$acode=$_POST['encrypt'];
$query5 = mysqli_query($dbc,"SELECT * FROM users where passreset='$acode'") or die(mysqli_error($dbc));
$list = mysqli_fetch_array($query5); /* Error-----*/
$val = $list['email'];
echo $val;?>" >
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password" placeholder="Password" >
</div>
<div class="form-group">
<label for="password">Re-enter Password</label>
<input type="password" class="form-control" id="password" name="Rpassword" placeholder="Password" >
</div>
<input type="hidden" class="form-control" name="encrypt" value="<?php echo $_GET['encrypt'];?>" >
<button class="btn btn-success" type="submit" name="submit" />Submit</button>
</form>

Categories