Jump to content

how to change a class in php tags ?


EarlyDude

Recommended Posts

I am trying to change the class of a button if a conditon is not met (   if(strlen($username) < 5)    ) this is my code :

 

 

 

 

<?php $buttonClass = 'btn btn-primary'; ?>
 
<?php
 
 
if(isset($_POST['name']) && $_POST['name'] != ""){
 
    $username = preg_replace('#[^a-z0-9]#i', '', $_POST['name']);
    $quer = "SELECT * FROM users WHERE username = '$username'";
    $resu = mysqli_query($dbc,$quer);
    $check = mysqli_num_rows($resu);
    if(strlen($username) < 5){
      echo '<span  class = "glyphicon glyphicon-remove" style = "color:red"> More then 4 caracters </span>';
      
      $buttonClass = 'btn btn-primary disabled';
   exit();
      
    }
 
    elseif(is_numeric($username[0])){
      echo '<span  class = "glyphicon glyphicon-remove" style = "color:red"> First character mst be a letter </span>';
      $buttonClass = 'btn btn-primary disabled';
      exit();
    }
 
 
 
    elseif($check < 1){
      echo '<span class = "glyphicon glyphicon-ok" style = "color:green"> ' . $username . ' is available</span>';
      
    }else{
 
      echo '<span  class = "glyphicon glyphicon-remove" style = "color:red"> This username is already taken </span>';
      $buttonClass = 'btn btn-primary disabled';
      exit();
    }
 
 
}
 
 
?>
 
<script type="text/javascript" language = "javascript">
 
function checkUsername () {
  var status = document.getElementById("usernamestatus");
  var u = document.getElementById("username").value;
  if(u != "")
  {
    
    status.innerHTML = 'Se verifica...';
    var hr = new XMLHttpRequest();
    hr.open("POST","register.php",true);
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    hr.onreadystatechange = function(){
        if(hr.readyState == 4 && hr.status == 200){
 
          status.innerHTML = hr.responseText;
 
        }
      }
 
 
  var v = "name="+u;
  hr.send(v);
       
 
 
 }
 
}
 
</script>
 
 
 
AND this is my html(under the php and the script)
 
<div class="form-group">
      <label for="username" class="col-lg-2 control-label">Username:</label>
      <div class="col-lg-10">
        <input type="text" name = "username" class="form-control" id="username" onkeyup="checkUsername()">
      
      <br><div class = "userspan">
             <span style = "font-size:15px"id = "usernamestatus"></span>
          </div>
</div>
    </div>
    <button type="reset" class="btn btn-default">Cancel</button>
        <button type="submit" class="<?php echo $buttonClass ?>" id = "submit" >Submit</button>
 
 
Now i know there is something to do with the exit() function but if i don't put it then the html apears on the page twice,how can i stop that from happening ? Please can someone help ?
 
Link to comment
https://forums.phpfreaks.com/topic/295628-how-to-change-a-class-in-php-tags/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.