This is my code:
<?php
$printhename = $_POST['username3'];
if(isset($_POST['username3']) && $printhename == "Carlos"){
echo "<h1 class='title123'>Hello $printhename </h1>";
}
else {
echo "<h1 class='title123'>You don't belong here!</h1>";
}
?>
What I want to do is, if "username3" is not set, the message "You don't belong here is shown". Otherwise, if the name is set and it's Carlos, display the other message. I must be doing this the wrong way because it is always printing "Hello $printhename", regardless of the name that has been input.
I'd like for someone to shed some light on this. I'm new to PHP.
If it helps, here's my HTML code:
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div class="centerlogin">
<form class="formlogin" name="login" method="POST" action="testing.php">
<input type="text" placeholder="username" NAME="username3">
<input type="submit" name="login_submit" value="login">
</form>
</div>
</div>
There's a few things wrong here.
Place the POST array that you first assigned for the variable and use the POST array for it to check if it's equal to "Carlos" inside the conditional statement.
<?php
// $printhename = $_POST['username3']; // this throws an undefined variable
if(isset($_POST['username3']) && $_POST['username3'] == "Carlos"){
$printhename = $_POST['username3'];
echo "<h1 class='title123'>Hello $printhename </h1>";
}
else {
echo "<h1 class='title123'>You don't belong here!</h1>";
}
?>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div class="centerlogin">
<form class="formlogin" name="login" method="POST" action="testing.php">
<input type="text" placeholder="username" NAME="username3">
<input type="submit" name="login_submit" value="login">
</form>
</div>
</div>
Remember, "Carlos" and "carlos" are two different animals.
On an added note; if you want to avoid showing the "You don't belong here!" message, you can check if the submit was set first; just in case your HTML and PHP are also in the same file.
I.e.:
<?php
if(isset($_POST['login_submit']) ){
if(!empty($_POST['username3']) && $_POST['username3'] == "Carlos"){
$printhename = $_POST['username3'];
echo "<h1 class='title123'>Hello $printhename </h1>";
}
else {
echo "<h1 class='title123'>You don't belong here!</h1>";
}
}
?>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div class="centerlogin">
<form class="formlogin" name="login" method="POST" action="testing.php">
<input type="text" placeholder="username" NAME="username3">
<input type="submit" name="login_submit" value="login">
</form>
</div>
</div>
Sidenote:
Use !empty()
instead of isset()
for the text input, it's actually better for those and it checks for both; if it's set and not empty.
So $_POST['username3'] will always be set because it's a field in your HTML form and will always be submitted by it. However, if the visitor did not fill the form field with a value, $_POST['username3'] will be empty.
So you probably want to test with empty($_POST['username3']) instead of isset($_POST['username3'])...
And since you are compare "not empty AND equals to 'Carlos'" you can totally drop the "not empty" condition. (If it's equal to Carlos, it's not empty!)
To recap, with the comment from Fred -ii-:
<?php
$printhename = $_POST['username3'];
if($printhename == "Carlos"){
echo "<h1 class='title123'>Hello $printhename </h1>";
}
else {
echo "<h1 class='title123'>You don't belong here!</h1>";
}
?>
Hope this helps!
Change your PHP to this:
<?php
if(isset($_POST['username3']))
{
$printhename = $_POST['username3'];
}
else
{
/* If it is not set, redirect client */
header('Location: /login.php');
}
if($printhename == "Carlos"){
echo "<h1 class='title123'>Hello $printhename </h1>";
}
else {
echo "<h1 class='title123'>You don't belong here!</h1>";
}
?>
Thank you so much for your replies — they really helped me a lot and I couldn't be more thankful.
I don't know why I changed the method to GET, it was POST before! To achieve the look I wanted, I had to work the code like this.
Inbetween the "head" tags:
<?php
$printhename = $_POST['username3'];
?>
The body:
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<div class="centerlogin">
<?php
if($printhename == "Carlos"){
echo "<h1 class='title123'>Hello $printhename </h1>";
}
if (isset($printhename) && $printhename != "Carlos") {
echo "<h1 class='title123'>You don't belong here!</h1>";
}
if(empty($printhename)) {
echo "<h1 class='title'>Log in, please</h1>";
}
?>
<form class="formlogin" name="login" method="POST" action="testing.php">
<input type="text" placeholder="username" NAME="username3">
<input type="submit" name="login_submit" value="login">
</form>
</div>
</div>
Is there any optimizations I could do?