2017-01-18 66 views
0

1.我已經創建了一個登錄表單,並且想要顯示已經使用的用戶名,請選擇其他名稱。想要在PHP中改變回聲的位置。我已經寫在PHP代碼中的回聲,但我想在HTML中顯示該回聲。如何顯示?如何在php中更改'echo'位置?

<?php 

    if (isset($_POST["sign_up"])) { 
     $fname = $_POST["first_name"]; 
     $lname = $_POST["last_name"]; 
     $email = $_POST["email"]; 
     $user = $_POST["username"]; 
     $password = $_POST["password"]; 
     $login_id = $row["login_id"]; 
     $sql_email = "SELECT email FROM login WHERE email='$email'"; 
     $email_result = mysqli_query($conn, $sql_email); 
     $email_num = mysqli_num_rows($email_result); 
     if ($email_num != 0) { //If there is already such username... // 
      $email_echo = '<span style="color:#e60000;text-align:center;">"There is user with that email.</span>'; // ...kill the script! 
      $email_echo = 'There is user with that email.'; 
     } else { //conti 
      $sql_signUp = "INSERT INTO login(first_name, last_name, email, username, password) 
      values('$fname', '$lname', '$email', '$user', '$password')"; mysqli_query($conn, $sql_signUp); 
     } 

     $sql_username = "SELECT username FROM login WHERE username='$user'"; //looking through existing usernames 
     $username_result = mysqli_query($conn, $sql_username); 
     $username_num = mysqli_num_rows($username_result); 

     if ($username_num != 0) { //If there is already such username... 
      $username_echo = '<span style="color:#e60000;text-align:center;">"There is user with that username.</span>'; // ...kill the script! 
     } else { //conti 
      $sql_signUp = "INSERT INTO login(first_name, last_name, email, username, password) values('$fname', '$lname', '$email', '$user', '$password')"; 
      mysqli_query($conn, $sql_signUp); 
     } 
    } 

?> 
    <div id="signup"> 
     <h1>Sign Up</h1> 
     <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> 
      <div class="top-row"> 
      <div class="field-wrap"> <label> First Name<span class="req">*</span> </label> <input type="text" required autocomplete="on" name="first_name"/> </div> 
      <div class="field-wrap"> <label> Last Name<span class="req">*</span> </label> <input type="text"required autocomplete="on" name="last_name"/> </div> 
      </div> 
      <div class="field-wrap"> <label> Email Address<span class="req">*</span> </label> <input type="email"required autocomplete="on" name="email"/> <?php echo $email_echo //display some PHP here?> </div> 
      <div class="field-wrap"> <label> Username<span class="req">*</span> </label> <input type="text" required autocomplete="on" name="username"/> </div> 
      <div class="field-wrap"> <label> Set A Password<span class="req">*</span> </label> <input type="password"required autocomplete="off" name="password"/> </div> 
      <button type="submit" class="button button-block" value="sign_up" name="sign_up"/>Register</button> 
     </form> 
    </div> 
+1

能不能請你格式化你的代碼正確 – elementzero23

+0

不明白什麼的話,能否請您正確格式化你的代碼? – AShly

回答

1

只是檢查username_echo變量被設置或不

<div id="signup"> 

     <h1>Sign Up</h1> 
     <h4><?php if(isset($username_echo) && !empty($username_echo)) echo $username_echo;?></h4> 
     <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> 
      <div class="top-row"> 
      <div class="field-wrap"> <label> First Name<span class="req">*</span> </label> <input type="text" required autocomplete="on" name="first_name"/> </div> 
      <div class="field-wrap"> <label> Last Name<span class="req">*</span> </label> <input type="text"required autocomplete="on" name="last_name"/> </div> 
      </div> 
      <div class="field-wrap"> <label> Email Address<span class="req">*</span> </label> <input type="email"required autocomplete="on" name="email"/> <?php echo $email_echo //display some PHP here?> </div> 
      <div class="field-wrap"> <label> Username<span class="req">*</span> </label> <input type="text" required autocomplete="on" name="username"/> </div> 
      <div class="field-wrap"> <label> Set A Password<span class="req">*</span> </label> <input type="password"required autocomplete="off" name="password"/> </div> 
      <button type="submit" class="button button-block" value="sign_up" name="sign_up"/>Register</button> 
     </form> 
    </div> 
+0

謝謝,它幫助我.. –