Topic: Help with form validation

RoffDaniel pro asked 6 years ago

Hello. I have a problem with form validation because i don't know JS. So I'm asking for help.

Here's the code:

 $errors = array();
 if ($data['server'] != "" || isset($data['server']))
 $user = R::findOne($data['server'], 'name = ?', array($data['name']));
 if( $user)
 if(validate_password($data['password'], $user->password))
 $_SESSION['logged_user'] = $user;
 } else {
 $errors[] = 'Неверный пароль!';
 } else
 $errors[] = 'Аккаунт с таким логином не найден или не существует!';
 } else
 $errors[] = 'Выберете сервер авторизации!';

And form:

<form method="POST" action="" id="jForm">
<div class="text-center">
<h3 class="dark-grey-text mb-5"><strong>Авторизация</strong></h3>
if (!empty($errors)) {
<div class="card red lighten-1 text-center z-depth-2">
<div class="card-body">
<p class="white-text mb-0">'.array_shift($errors).'</p>
<hr class="my-5">
<!--<label for="server" data-error="error">Сервер</label>-->
<select class="mdb-select colorful-select dropdown-primary" name="server" id="server">
<option value="" disabled selected>Выберите ваш сервер</option>
<option value="users_gov_orange">Orange</option>
<option value="users_gov_blue">Blue</option>

<div class="md-form">
<i class="fa fa-user prefix"></i>
<input type="text" id="name" name="name" class="form-control validate" value = "'.$data['name'].'">
<label for="name" data-error="error">Ваш ник</label>

<div class="md-form pb-3">
<i class="fa fa-lock prefix"></i>
<input type="password" id="password" name="password" class="form-control validate" value = "'.$data['password'].'">
<label for="password" data-error="error">Ваш пароль</label>

<!--<p class="font-small blue-text d-flex justify-content-end text-center"><a href="#" class="blue-text ml-1">восстановить пароль</a></p>-->

<div class="text-center mb-3">
<button type="submit" class="btn blue-gradient btn-block btn-rounded z-depth-1a" name="do_login" id="do_login">Войти</button>

Witold Tkaczyk free commented 6 years ago

Hi RoffDaniel, can you describe to me what exact problem do you have that I can find your problem? Any errors?

RoffDaniel pro commented 6 years ago

There are no errors. The question is, I don't know how to do validation because I don't know JS, and I'm asking you to try to do it for me, if you can.

Witold Tkaczyk free commented 6 years ago

You don't need to know js to do validation. Follow this or many others tutorials.

RoffDaniel pro commented 6 years ago

Thank you, but still, how to make that at request in which there is an error, the page was not reloaded and the attribute "data-error" was called ?

Bartłomiej Malanowski staff commented 6 years ago

Create an AJAX request. In the response, you need to tell the script if the field is valid or invalid. If the field is invalid, you can simply add `.invalid` class to your input. You can also set the message by editing the label's attribute "data-error"


Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.



Specification of the issue

  • ForumUser: Pro
  • Premium support: No
  • Technology: MDB jQuery
  • MDB Version: -
  • Device: -
  • Browser: -
  • OS: -
  • Provided sample code: No
  • Provided link: No