How to change the text of a p within php? Can you insert a snippet in JavaScript?

0

I am making a registration in which can not have users with the same name / nick registered. To validate whether or not I use PHP, I would like to know how to print a p with a message on the screen if a user with that name already exists. I tried to do this, but it does not work

    if(is_null($id)){
            $sql = "INSERT INTO usuario (nick, email, senha) VALUES ('$nick' , '$email' ,'$senha')";
            $q = mysqli_query($con,$sql);
            $url = 'aux.php';
            echo'<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
         }
         else{
            ?>
             <script type='text/javascript'>
                 var uso = document.getElementById('emUso');
                 uso.innerHTML = 'Este nick já está em uso!'
             </script>
         <?php   
         }
    
asked by anonymous 14.06.2016 / 04:35

1 answer

1

$(document).ready(function() {
  $("input[type='text']").on('change', function() {
    $.ajax({
      type: "POST",
      url: "suaurl.php",
      data: {
        'nickName': $.trim($(this).val())
      },
      dataType: 'json'
    }).done(function(response) {
      if (!response.status) {
        alert('Já existe alguém com esse nickName');
        return false;
      }
      // faça algo
    });
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><label>Nickname</label><inputtype="text">

As I do not know the architecture of your project, whether you are working with class or structured. I'll leave a very basic example. Understand the idea >

<?php
    $getNickname = getNickName($valorDoInput); // faz sua consulta
    if(count($getNickname)){ //conta o array, se for maior que 0, entra no if
       echo json_enconde(array('status' => false));// retorna status false para o Ajax
       exit;
    }
    
14.06.2016 / 05:13