Log in

View Full Version : Resolved echo not working



Kage Kazumi
10-21-2012, 09:35 AM
I'm creating the login page for my admin area to my CMS I am working on. I am using the "if" statement to echo instructions if the form input fields are blank. However, the echo will not work inside the "if" statement. I tried moving the echo outside of the "if" statement and it works fine and echos like it should except when inside the if statement.

admin-login.php

PHP


<?php include_once ('include/scripts/login.php'); ?>


HTML


<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="style/css/style.css"></link>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
</head>

<body>
<!--START: Log In Wrapper-->
<div id="login-wrapper">
<!--START: Error Display-->
<div id="login-errors">
&nbsp;
</div>
<!--END: Error Display-->
<!--START: Log In Form-->
<div id="login-form">
<h2>Admin Login Panel</h2><!--Login Header-->
<!--START: Login Form-->
<form method="post" action="admin-login.php">
<!-- START: Form Element USERNAME-->
<div class="form-element">
<label for="Username" class="admin-login-label">Username</label><!--Username Label-->
<input type="text" name="username" placeholder="Enter Username" id="admin-username"/><!--Username Input-->
</div>
<!-- END: Form Element USERNAME-->
<!-- START: Form Element PASSWORD-->
<div class="form-element">
<label for="Password" class="admin-login-label">Password</label><!--Password Label-->
<input type="text" name="password" placeholder="Enter Passowrd" id="admin-password"/><!--Password Input-->
</div>
<!-- END: Form Element PASSWORD-->
<!-- START: Form Element SUBMIT-->
<div class="form-element">
<label></label><!--Leave Blank-->
<input type="Submit" name="logintoadmincenter" value="Login" id="admin-login-button" /><!--Username Input-->
</div>
<!-- END: Form Element SUBMIT-->
</form>
<!--END: Login Form-->
</div>
<!--END: Log In Form-->
</div>
<!--END: Log In Wrapper-->
</body>
</html>


login.php



<?php

if(isset($_post['logintoadmincenter'])) {

include_once '.:include/connection.php';
$username = $_post['username'];
$password = $_post['password'];
if (empty($username) || empty($password)) {

echo ("Please fill in the required fields");

}

}
?>


I receive no errors. It just doesn't echo, even if written as:



echo ('Please fill in the required fields');


OR



echo "Please fill in the required fields";


OR



echo 'Please fill in the required fields';

keyboard
10-21-2012, 09:49 AM
That means that the statement is never returning true.

Just before the if statement, echo out the values of $username, $password and their respective lengths.

Kage Kazumi
10-21-2012, 10:05 AM
That means that the statement is never returning true.

Just before the if statement, echo out the values of $username, $password and their respective lengths.

Being a beginner you have only confused me more. What do you mean by "echo out the values?"

keyboard
10-21-2012, 11:29 AM
Put in this before the if statement

echo $username . '<br />';
echo $password . '<br />';
echo strlen($username) . '<br />';
echo strlen($password) . '<br />';

And tell me what the four values are...

Kage Kazumi
10-21-2012, 05:33 PM
I just get a couple of errors that say:

Undefined variable: username

Undefined variable: password

If it is above both if statements.

However, above the second if statement it does nothing and echos nothing.

EDIT

When I put the variables and the echos above both "if" statements I get two errors:

Undefined index: password
Undefined index: username

However, it does echo two zeros below the errors.

traq
10-21-2012, 07:00 PM
When I put the variables and the echos above both "if" statements I get two errors:

Undefined index: password
Undefined index: usernamethat would imply that your form is not submitting those fields...

try changing this
if (empty($username) || empty($password)) {

echo ("Please fill in the required fields");

} to this
if (empty($username) || empty($password)) {

echo "Please fill in the required fields";

}else{
print "<pre>";
var_dump( $_POST );
}

Kage Kazumi
10-21-2012, 07:19 PM
I thanks for the help. However, I went ahead and started working on it adding more information. So the 'echo' is not there. I will post again if I receive any new errors regarding the echo later in the script.

EDIT

Oh man I am an idiot. I set the error to display only if the button was pressed. So yes it does work. :facepalm:

traq
10-21-2012, 08:07 PM
if(isset($_post['logintoadmincenter'])) {
include_once '.:include/connection.php';
$username = $_post['username'];
$password = $_post['password'];
if (empty($username) || empty($password)) {
echo ("Please fill in the required fields");
}
}

Also (and I'm sorry for not mentioning this earlier; I completely overlooked it):
$_POST (the automatically created superglobal that holds HTTP POST data) is not the same as $_post (which will only exist if you create it, and holds only what you assign to it).