How to change the text of a <p> inside php? Can you insert a snippet in Javascript?

Asked

Viewed 159 times

0

I am making a registration that cannot have users with the same name/nick registered. To validate whether or not PHP is used, I would like to know how to print a p with a message on the screen if there is already a user with that registered name. I tried to do it like this, but it doesn’t 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   
         }
  • For these reasons php has a bad reputation. To solve your problem, look for Ajax requests in javascript. You can make a query in the database to see if this nick name already exists every time you change the input value, if it returns false, generate an error msg or something like that. The way you’re doing besides being wrong, it’s bad practice.

  • uses Ajaxassociated with jquery bind

  • Hmm, okay. Thank you very much!

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>Nick name</label>
<input type="text">

As I don’t know the architecture of your project, whether you are working with class or structured. I’ll leave a super 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;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.