This is a slightly simple question, however I cannot recall how to control PHP output within an HTML page. More specifically, I want to control the position of an echo
statement (part of an else
statement) using css and tags within the HTML:
if($row == 1)
{
echo '<div id="errormsg">This username is already taken</div>';
}
else
{
$add = mysqli_query($dbcon, "INSERT INTO users (id, firstname, lastname, username, password, admin) VALUES
(null, '$fname', '$lname', '$user', '$pass', '$admin') ") or die ("Can't insert data");
echo '<div id="create-success">Successfully added user!</div>';
}
I can control some things using the internal <div>
tags as shown above, but how can I have it positioned in relation to the HTML?
For a quick fix, you can do:
if($row == 1)
{
$error = '<div id="errormsg">This username is already taken</div>';
}
else
{
$add = mysqli_query($dbcon, "INSERT INTO users (id, firstname, lastname, username, password, admin) VALUES
(null, '$fname', '$lname', '$user', '$pass', '$admin') ") or die ("Can't insert data");
echo '<div id="create-success">Successfully added user!</div>';
}
and use the $error
variable where you want to display it, e.g., in your view.php file:
<?php if(isset($error)){ echo $error; } ?>
This is just a quick fix, please learn more about MVC and how to separate your view from your logic.
In your PHP code you have successfully added id's.
You need to add styling for these IDs in your stylesheet (incase either are called).
For example.
#errormsg{
//css here
}
#create-success{
//css here
}
Not sure if this is what you meant, but supposed you have your HTML page and PHP code inside it:
<div>
<?php
if ($row == 1) {
echo "This username is already taken";
} else {
$add = mysqli_query($dbcon, "INSERT INTO users (id, firstname, lastname, username, password, admin) VALUES
(null, '$fname', '$lname', '$user', '$pass', '$admin') ") or die ("Can't insert data");
echo "Successfully added user!";
}
?>
</div>
Whatever your result from the coding side is, you just put on the conditions, or use the code as follows:
if($row == 1)
{
$msg = '<div id="errormsg">This username is already taken</div>';
}
else
{
$add = mysqli_query($dbcon, "INSERT INTO users (id, firstname, lastname, username, password, admin) VALUES (null, '$fname', '$lname', '$user', '$pass', '$admin') ") or die ("Can't insert data");
if($add) {
$msg = '<div id="create-success">Successfully added user!</div>';
} else {
$msg = 'Error in adding record';
}
}
and finally use the $msg
variable in your PHP or HTML side as follows:
<?php if($msg) { echo $msg; }?>