Does not write an error that the login is busy and that the fields are empty, always writes that they are successfully registered. In the database enters if the data is correct.
Reg.php file

<?php include("include/db_connect.php"); include("functions/functions.php"); ?> <!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content="text/html" /> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/> <link href="css/reset.css" rel="stylesheet" type="text/css" /> <link href="css/style.css" rel="stylesheet" type="text/css" /> <title>Регистрация</title> </head> <body> <div id="block-body"> <?php include("include/block-header.php"); ?> <div id="block-cont"> <form method="post" id="form_reg" action="/reg/handler_reg.php"> <p id="reg_message"></p> <div id="block-form-reg"> <div class="form-group"> <label for="inputName">Логин</label> <input type="text" class="form-control" name="reg_login" id="reg_login" placeholder="Введите Логин" /> </div> <div class="form-group"> <label for="inputPass">Пароль</label> <input type="text" class="form-control" name="reg_pass" id="reg_pass" placeholder="Введите Пароль" /> </div> <div class="form-group"> <label for="inputEmail">Email</label> <input type="email" class="form-control" name="reg_email" id="reg_email" placeholder="Введите Email" /> </div> <input type="submit" name="reg_submit" id="reg_submit" value="Регистраиця" /> </div> </form> </div> <?php include("include/block-footer.php"); ?> <script type="text/javascript" src="/js/shop-script.js"></script> <script type="text/javascript" src="/js/jquery.form.js"></script> <script type="text/javascript" src="/js/jquery-3.1.1.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script> <script> $("#form_reg").submit(function(event) { event.preventDefault(); // прерываем отправку формы var data = $('#form_reg').serialize(); $.ajax({ type: "POST", url:"/reg/handler_reg.php", data:data, error:function(){ $("#reg_message").addClass("reg_mess_error").fadeIn(400).html(data); }, beforeSend: function() { $("#reg_message").html('Загрузка...'); }, success: function(html){ $("#block-form-reg").fadeOut(300); $("#reg_message").addClass("reg_mess_good").fadeIn(400).html("Вы успешно зарегистрированы!"); } }); return false; }); </script> </body> </html> 

File handler_reg.php

  session_start(); include("../include/db_connect.php"); include("../functions/functions.php"); $error = array(); $login = iconv("UTF-8", "cp1251",strtolower(clear_string($_POST['reg_login']))); $pass = iconv("UTF-8", "cp1251",strtolower(clear_string($_POST['reg_pass']))); $email = iconv("UTF-8", "cp1251",clear_string($_POST['reg_email'])); if (strlen($login) < 5 or strlen($login) > 15) { $error[] = "Логин должен быть от 5 до 15 символов!"; } else { $result = mysql_query("SELECT login FROM reg_user WHERE login = '$login'",$link); If (mysql_num_rows($result) > 0) { $error[] = "Логин занят!"; } } if (strlen($pass) < 7 or strlen($pass) > 15) $error[] = "Укажите пароль от 7 до 15 символов!"; if (!preg_match("/^(?:[a-z0-9]+(?:[-_.]?[a-z0-9]+)?@[a-z0-9_.-]+(?:\.?[a-z0-9]+)?\.[az]{2,5})$/i",trim($email))) $error[] = "Укажите корректный email!"; if (count($error)) { echo implode('<br />',$error); }else { $pass = md5($pass); $pass = strrev($pass); $pass = "9nm2rv8q".$pass."2yo6z"; $ip = $_SERVER['REMOTE_ADDR']; mysql_query(" INSERT INTO reg_user(login,pass,email) VALUES( '".$login."','".$pass."','".$email."')",$link); echo 'Регистрация прошла успешно! <a href="index.php">Перейти на главную страницу.</a> '; } ?> 
  • handler_reg.php where? - Naumov
  • Oh sorry, added - Artem Fomkin
  • if (count($error)) - what are you checking here? - Dmitriy Kondratiuk
  • number of elements in the $ error array - Alexey Shatrov
  • OK, if there are no items in the array, what will it return? - Dmitriy Kondratiuk

2 answers 2

This is how it should work:

 if (count($error)>0) //если в масиве есть елементы { echo implode('<br />',$error); }else { $pass = md5($pass); $pass = strrev($pass); $pass = "9nm2rv8q".$pass."2yo6z"; $ip = $_SERVER['REMOTE_ADDR']; $addDB = mysql_query(" INSERT INTO reg_user(login,pass,email) VALUES( '".$login."','".$pass."','".$email."')",$link); if($addDB) // если переменная $addDB существует то запрос к базе выполнен {echo 'Регистрация прошла успешно! <a href="index.php">Перейти на главную страницу.</a> ';} else { echo 'Запись не внесена в базу';} } 
  • still displays a message that they successfully registered - Artem Fomkin
  • Ajax problem seems to me, but I do not have enough knowledge to understand - Artem Fomkin
  • try instead of $("#reg_message").addClass("reg_mess_good").fadeIn(400).html("Вы успешно зарегистрированы!"); write $("#reg_message").addClass("reg_mess_good").fadeIn(400).html(html); Should display a message received from handler_reg.php - Dmitriy Kondratiuk
  • Who are you Dmitry when the error is triggered in ajax? - Naumov
  • Dmitriy Kondratiuk, thanks a lot, helped, began to display errors from handler_reg - Artem Fomkin

it

  if (count($error)) { echo implode('<br />',$error); } 

we change for it:

  if (count($error)) { http_response_code(409); echo implode('<br />',$error); } 

but here you should do how to return all json with the flag of error or correctness.

ps I will not delete it because in ajax callback error is triggered if the http error code was returned.

  • replaced issued an error Fatal error: Call to undefined function http_response_code () - Artem Fomkin
  • @Artemfomkin header("HTTP/1.0 409 Conflict"); you can replace this with php 5.3 apparently some sort of - Naumov
  • Yes, I have php 5.3, now when I click the register above, reg_login = ®_pass = ®_email = ®_captcha = - Artem Fomkin
  • @Artemfomkin I do not understand something like the answer above you fit? - Naumov
  • I wanted to see your method - Artem Fomkin