Reputation: 65
1.I have created a login form and want to display username already in use please select another name. Want to change the position of echo in php. I have written echo in php code but i want to display that echo in html. How to display that?
<?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>
Upvotes: 0
Views: 1793
Reputation: 1320
Just check username_echo variable is set or not
<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>
Upvotes: 1