Php $ _POST output error

I'm trying to perform simple actions in web form in PHP, any input, said output value in the input field 1

. and undefined index error for username

, password

when isset

there is no prior$_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;
?>

      

+3


source to share


10 replies


<?php
    /*** if else in a single statement ****/
    $username = isset($_POST["username"]) ? $_POST["username"] : "";
    $pass     = isset($_POST["password"]) ? $_POST["password"] : "";
    echo $username." - ".$pass;
?>

      



+11


source


Do not use

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

      

Put only



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

      

Since isset () gives boolean values, that is 1 or 0. In your case, isset ($ _ POST ["username"]) checks if you put values ​​in this one or not. if there are some values ​​in the username field then it returns 1, otherwise it returns 0.

+7


source


It should be

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

      

isset($_POST["username"])

and isset($_POST["password"])

return 1

if evaluated TRUE

, since it isset()

returns a boolean.

+5


source


Others told you how to fix the problem. I'll tell you what the problem is.

Problems

You need to know the following things:

PHP boolean for string implicit conversion

An implicit conversion to a string of logic, the PHP acts a little strange .

$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 does it work isset()

According to PHP Manual ,

Returns TRUE

if [variable supplied as an argument] exists and has a non-NULL value, FALSE otherwise.

What happens when you try to access the $ _POST parameter

As you know perfectly well, parameters $_POST

are available for PHP script when

  • There is an HTML form from which the attribute action

    points to the PHP script
  • The user submits the form with or without data.

If the name

field attribute was on the form "username"

, you can access this information from the PHP script after submitting the form as follows:

$_POST["username"]

      

But if the PHP script that processes the parameters $_POST

is in the same file as the HTML form, the parameters $_POST

will be available to the script the first time it is accessed.

And if you tried to access the parameters $_POST

when they are not there, the result will be Undefined index

.

What's going on here?

With the understanding of the above three things, we can infer what is happening in your case.

The first time your file is loaded into the browser, the PHP interpreter must interpret it first. But this first time there are variables $_POST

. So when you try to access the parameters $_POST

it will throw an error Undefined index

.

When you terminate the expression $_POST["..."]

in isset()

, isset()

will return FALSE

, because there are simply no parameters. Since it FALSE

converts to an empty string ( ""

), your statement echo

doesn't produce anything visible.

When the form is submitted, the calls isset()

return TRUE

because this time there are parameters $_POST

. And as described above, TRUE

becomes "1"

when converted to strings. Hence, you get an output 1

.

Decision

$_POST

You should always check before accessing parameters .

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

      

Reduction:

$username = isset($_POST["username"]) ? $_POST["username"] : "";

      

+3


source


Why are you using isset($_POST["username"])

? The function isset()

returns false if the test variable contains NULL and true otherwise.

Try

if(isset($_POST))
{
   $usernmae=$_POST["username"];
   $pass=$_POST["password"];
   echo $usernmae," ",$pass;
}

      

+2


source


You need to check if the first value exists. If you don't, you will receive an error message. this way isset($_POST['VALUE'])

If it exists, get it. If not, set the value 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;
?>

      

+2


source


Your problem is this line

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

      

You are storing a value isset()

that is boolean 0 or 1.

Since 2 parameters are set, you get 1 (true).

You want to do this:

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

      

and the same for the password.

+2


source


try it. and the reason you are getting 1 and 1 is because you are using

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

      

This is the one thing that you installed it.

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

      

+2


source


Replace the bottom 3 lines with

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

      

The isset () function returns a boolean value, whether the variable is set or not. This makes it return 1. In my answer, if the field has a value it will echo the value, if not, there will be an empty line echo

+2


source


 if(isset($_POST) && !empty($_POST))
 {
  $usernmae=$_POST["username"];
  $pass=$_POST["password"];
  echo "User Name is : ".$usernmae,"Password is : ".$pass;
 }

      

+1


source







All Articles