Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying simple web form action in PHP, whatever input given to in the input field output value is 1. and undefined index error for username, password when isset not present before the $_POST

<html>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit"></form>
</html>

<?php
$usernmae=isset($_POST["username"]);
$pass=isset($_POST["password"]);
echo $usernmae," ",$pass;
?>
share|improve this question
2  
remove isset function –  Gowri 11 hours ago
1  
Quick tip: <form action="" method="post"> will post back to the current URL, which is useful if you decide to do "pretty URLs" later on. It also preserves all query string parameters. –  Niet the Dark Absol 8 hours ago

9 Answers 9

up vote 10 down vote accepted
$username=isset($_POST["username"])?$_POST["username"]:"";
$pass=isset($_POST["password"])?$_POST["password"]:"";
echo $username." - ".$pass;
share|improve this answer
    
This is a good suggestion, but you should provide some explanation. –  Yogu 5 hours ago

Dont use

$usernmae=isset($_POST["username"]);
$pass=isset($_POST["password"]);

Put only

$usernmae=$_POST["username"];
$pass=$_POST["password"];

Because isset() gives boolean values i.e. 1 or 0. In your case isset($_POST["username"]) checks that you have put values in this field or not. if there is some values in username field then it return 1 otherwise returns 0.

share|improve this answer
1  
improve your answer by elaborating why :) –  Magic-Mouse 11 hours ago
    
isset() does not "give 1 or 0". It returns TRUE or FALSE. See my answer for an explanation. –  Krumia 5 hours ago

It should be

$usernmae= $_POST["username"];
$pass= $_POST["password"];
echo $usernmae," ",$pass;

isset($_POST["username"]) and isset($_POST["password"]) return 1 if evaluation is TRUE since isset() return boolean.

share|improve this answer

You need to check if a value exists first. If you don't, you will get an error. thus isset($_POST['VALUE']) If it exists, get it. If not, set to "empty"

<html>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit"></form>
</html>
<?php
/**
 * Created by PhpStorm.
 * User: Devunne3
 * Date: 25/09/14
 * Time: 12:32
 */
$usernmae=isset($_POST["username"])?$_POST['username']:'';
$pass=isset($_POST["password"])?$_POST['password']:'';
echo $usernmae," ",$pass;
?>
share|improve this answer

Why you are using isset($_POST["username"])? The isset() function return false if testing variable contains a NULL value and true otherwise.

Try like

if(isset($_POST))
{
   $usernmae=$_POST["username"];
   $pass=$_POST["password"];
   echo $usernmae," ",$pass;
}
share|improve this answer

Your problem is this line

$usernmae=isset($_POST["username"]);
$pass=isset($_POST["password"]);

You're saving the value of isset(), which is a boolean 0 or 1.

Since the 2 params are set you're getting the 1 (true).

You want to do this:

if(isset($_POST["username"])){
    $usernmae = $_POST['username'];
}

and the same for the password.

share|improve this answer

Replace the bottom 3 lines with

$username=isset($_POST['username'])?$_POST['username']:'';
$pass=isset($_POST['password'])?$_POST['password']:'';
echo $username," ",$pass

The isset() returns boolean, whether the variable is set or not. That makes it return 1. In my answer if the field has value, it will echo the value if not, will echo blank string

share|improve this answer

try this. and the reason you you are getting 1 and 1, because you are using

$usernmae=isset($_POST["username"]);

That is one couase you set it.

if(!empty($_POST['submit'])){
   $usernmae=$_POST["username"];
   $pass=$_POST["password"];
   echo $usernmae," ",$pass;
}
share|improve this answer

Others have told you how to solve the problem. I will tell you what the problem is.

Problems

You need to know following things:

PHP's boolean to string implicit conversion

When implicitly converting a boolean to a string, PHP acts a bit weird.

$testVar = false;
echo (string)$testVar; //will output a empty string (as that evaluates to false)
$testVar = true;
echo (string)$testVar; //will output 1 (as that evaluates to true)

How isset() works

According to the PHP Manual,

Returns TRUE if [variable supplied as argument] exists and has value other than NULL, FALSE otherwise.

What happens when you try to access a $_POST parameter

As you very well know, $_POST parameters are available to a PHP script when,

  1. There is an HTML form, of which action attribute points to the PHP script
  2. User submits the form, with or without data

If the name attribute of a field in the form was "username", you can access this information from the PHP script after the submission of the form like this:

$_POST["username"]

But if the PHP script that processes the $_POST parameters are in the same file as the HTML form, no $_POST parameters will be available to the script when it is initially accessed.

And if you tried to access $_POST parameters when there are none, the result is Undefined index.

What's happening here?

With the understanding of the above three things, we can deduce what's happening in your case.

When your file is loaded on the first time to the browser, PHP interpreter has to interpret it first. But on this first time, no $_POST variables are present. So when you try to access $_POST parameters, it will result in an Undefined index error.

When you wrap the $_POST["..."] expressions in an isset(), isset() will return FALSE, because the parameters are simply not there. Since FALSE is converted to empty string (""), your echo statement produces nothing visible.

When you submit the form, the isset() calls will return TRUE, because the $_POST parameters are present this time. And as described above, TRUE becomes "1" when converted to strings. Hence you get the output 1.

Solution

You must always check before you access $_POST parameters.

$username = "";
if (isset($_POST["username"])) {
    $username = $_POST["username"];
}

Or shorthand:

$username = isset($_POST["username"]) ? $_POST["username"] : "";
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.