简体   繁体   中英

I want show message about existing username under username input tag and when show the message other the data doesn't lose in php

I want show message about existing username under username input tag and when show the message other the data doesn't lose. But this codes don't work.

Here is my code for existing username:

      <?php
            //click register button
            if(isset($_POST['register']))
            {
                //Retrieve the field values from our registration form.
                    $username = $_POST['username'];

            //Now, we need to check if the supplied username already exists.

            $sql = "SELECT COUNT(user_username) AS num FROM users WHERE user_username = :username";
            $stmt = $dbh->prepare($sql);

            $stmt->bindValue(':username', $username);

            $stmt->execute();

            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            if($row['num'] > 0){

               echo "<script>";
               echo "$(document).ready(function(){document.getElementById('existing_username').innerHTML = 'existing'})";
               echo "</script> ";

//the following code is work but other data like name, password is lost in input element                     
//echo '<div class="alert alert-danger"> <b>'.$username.'</b> This username already exist!</div>'; 
                } }  
    ?>

And here is my code for input data:

 <form action="register.php" method="post">
               <div class="row"> 
                  <div class="col-sm-6">  
                     <div class="form-group">          
                        <label for="username"><b>USERNAME</b></label> 
                         <input class="form-control" type="text" id="username" name="username">
                       </div>
                   </div>
                </div>
<span class="text-danger" id="existing_username"></span>
    <button class="btn btn-primary" type="submit" name="register" value="Register">Register</button>
            </form>

when show the message other the data doesn't lose

What data?!

ok,so you should store the error message which is Username exist in a variable,like below :

<?php
     //click register button
            if(isset($_POST['register']))
            {
                //Retrieve the field values from our registration form.
                    $username = $_POST['username'];

            //Now, we need to check if the supplied username already exists.

            $sql = "SELECT COUNT(user_username) AS num FROM users WHERE user_username = :username";
            $stmt = $dbh->prepare($sql);

            $stmt->bindValue(':username', $username);

            $stmt->execute();

            $row = $stmt->fetch(PDO::FETCH_ASSOC);
            if($row['num'] > 0){

               echo "<script>";
               echo "$(document).ready(function(){document.getElementById('existing_username').innerHTML = 'existing'})";
               echo "</script> ";


                $error = "<b>".$username."</b> :This username already exist!"; 
//this code is work but other data is lost

                } }  

?>

<form action="register.php" method="post">
       <div class="row"> 
          <div class="col-sm-6">  
             <div class="form-group">          
                <label for="username"><b>USERNAME</b></label> 
                 <input class="form-control" type="text" id="username" name="username">
               </div>
           </div>
        </div>
</form>

Check this answers too Embed Php in Html

You need to provide value attribute for your form fields. For example:

<input class="form-control" type="text" id="username" name="username" value="<?php echo $username ?>">

OR

<input class="form-control" type="text" id="name" name="name" value="<?php echo $name ?>">

etc.

Of course you need to define these variables in you code first. If form have been submitted - take them from $_POST. If not - set them as empty values.

Code example with 2 fields:

<?php

$name = null;
$username = null;

//click register button
if (isset($_POST['register'])) {
    //Retrieve the field values from our registration form.
    $name = $_POST['name'];
    $username = $_POST['username'];

    //Now, we need to check if the supplied username already exists.

    $sql = "SELECT COUNT(user_username) AS num FROM users WHERE user_username = :username";
    $stmt = $dbh->prepare($sql);

    $stmt->bindValue(':username', $username);

    $stmt->execute();

    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    if ($row['num'] > 0) {

        echo "<script>";
        echo "$(document).ready(function(){document.getElementById('existing_username').innerHTML = 'existing'})";
        echo "</script> ";

        $error = "<b>".$username."</b> :This username already exist!";
        //this code is work but other data is lost
    }
}

?>

<form action="register.php" method="post">
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <label for="name"><b>NAME</b></label>
                <input class="form-control" type="text" id="name" name="name" value="<?php echo $name ?>">
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <label for="username"><b>USERNAME</b></label>
                <input class="form-control" type="text" id="username" name="username" value="<?php echo $username ?>">
            </div>
        </div>
    </div>
    <span class="text-danger" id="existing_username"></span>
    <button class="btn btn-primary" type="submit" name="register" value="Register">Register</button>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM