Advertisement
The_Expendable

Blank field and space detection system.

Apr 17th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.06 KB | None | 0 0
  1. //IMPORTANTE!!!
  2. Para o código correr sem problemas é obrigatório fazer a inclusão das seguintes bibliotecas de javacript antes do código ajax: https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.css
  3. https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js
  4. O código para fazer a inclusão das bibliotecas deverá ser o seguinte:
  5. <script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/js/toastr.min.js"></script>
  6. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.css">
  7.  
  8. //AJAX NO login.php
  9.  
  10.             $(document).ready(function(){
  11.                 $('#reg_submit').bind('click', function(data){
  12.                     data.preventDefault();
  13.                     $.ajax({
  14.                         url: 'tratareg.php',
  15.                         dataType: 'json',
  16.                         data:
  17.                         {
  18.                             first_name: $('#first_name').val(),
  19.                             last_name: $('#last_name').val(),
  20.                             username: $('#username_reg').val(),
  21.                             email: $('#email_reg').val(),
  22.                             password: $('#password_reg').val(),
  23.                             password_conf: $('#password_conf').val()
  24.                         },
  25.                         type: 'POST',
  26.                         success: function(data) {
  27.                             if(data.status == 'success'){
  28.                             toastr.success('Registro feito com sucesso','Sucesso!');
  29.                             }else if(data.status == 'error'){
  30.                                 toastr.error('Não foi possível processar o pedido','Erro de ligação');
  31.                             }else if(data.status == 'usr_error'){
  32.                                 toastr.error("This user already exists!", "Erro!");
  33.                             }else if(data.status == 'passwd_error'){
  34.                                 toastr.error("The passwords don't match!", "Erro!");
  35.                             }else if(data.status == 'blank_space'){
  36.                                 toastr.error("Um ou mais campos estão em branco ou possuem espaços!", "Erro!");
  37.                             }
  38.                         }
  39.                     });
  40.                 });
  41.             });
  42.  
  43. //FIM DO AJAX
  44.  
  45. //NOTAS
  46.  
  47. O formulário em HTML não pode conter o parâmetro action, por exemplo, o formulário em HTML deverá ter a seguinte estrutura: <form method="POST"> </form>
  48.  
  49. '#reg_submit' é o ID do formulário de registo.
  50. O '#first_name', '#last_name', '#username_reg', etc. são os IDs dos campos do formulário.
  51.  
  52.  
  53. //PHP NO tratareg.php
  54.  
  55. <?php
  56.  
  57. $pass1 = $_POST['password'];
  58. $pass2 = $_POST['password_conf'];
  59. $user = $_POST['username'];
  60. $email = $_POST['email'];
  61. $first_name = $_POST['first_name'];
  62. $last_name = $_POST['last_name'];
  63. $string_array = array($pass1, $user, $email, $first_name, $last_name);
  64. $i = 0;
  65.  
  66. $conn = mysqli_connect('localhost','root','usbw','csgo_bm','3307');
  67.  
  68. $check = "SELECT * FROM users WHERE email='".$email."' OR username='".$user."'";
  69.  
  70. if($pass1 == $pass2)
  71. {
  72.     if(mysqli_num_rows(mysqli_query($conn, $check)) >= 1)
  73.     {
  74.         $response_array['status'] = 'usr_error';
  75.     }
  76.     else
  77.     {
  78.         while(list ($key, $val) = each ($string_array) ){
  79.             if ( preg_match('/\s/',$val) or $val == ""){
  80.                 $i = 1;
  81.             }
  82.         }
  83.  
  84.         if($i == 1){
  85.             $response_array['status'] = 'blank_space';
  86.         }
  87.         else{
  88.             $salt = "affb7edfc72316ce4ca9ba814cf6b71e0e331cd0c5391ee3d5c1589ac55fcecd";
  89.             $hashedPass = sha1($salt.$pass1);
  90.             $sql = 'INSERT INTO users (user_id, username, password, first_name, last_name, email)
  91.                     VALUES ("NULL",
  92.                             "'.$user.'",
  93.                             "'.$hashedPass.'",
  94.                             "'.$first_name.'",
  95.                             "'.$last_name.'",
  96.                             "'.$email.'")';
  97.  
  98.             if (mysqli_query($conn, $sql)) {
  99.                 $response_array['status'] = 'success';
  100.             }
  101.             else {
  102.                 $response_array['status'] = 'error';
  103.             }
  104.         }
  105.     }
  106. }
  107. else
  108. {
  109.     $response_array['status'] = 'passwd_error';
  110. }
  111.  
  112. header('Content-type: application/json');
  113. echo json_encode($response_array);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement