EarlyDude Posted April 17, 2015 Share Posted April 17, 2015 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 ? Quote Link to comment Share on other sites More sharing options...
Solution EarlyDude Posted April 17, 2015 Author Solution Share Posted April 17, 2015 (edited) i found out the answer,can this post be delleted please ? Edited April 17, 2015 by EarlyDude Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.