Jump to content

how to change a class in php tags ?


EarlyDude
Go to solution Solved by 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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.