user3417730
user3417730

Reputation:

PHP Undefined index in XAMPP?

My code is extremely simple, but I have no idea what I've done to cause this error.

Notice: Undefined index: value in C:\xampp\htdocs\index.php on line 8

<form name="shuffle" action="" method="POST">
    <input type="text" name="value">
    <input type="submit" value="Shuffle">
</form>

PHP code: echo str_shuffle($_POST['value']);

Upvotes: 0

Views: 630

Answers (2)

Soanix
Soanix

Reputation: 36

If you call $_POST['value'] when form has not been previously submitted you get a warning that the key of $_POST is not defined.

Try to define the variable anyway. So that if you have sent the form, take the value of the field, otherwise the value is FALSE

$value = isset($_POST['value']) ? $_POST['value'] : FALSE; //$value is always defined
if($value !== FALSE){
//something like
echo str_shuffle($value);
}

PHP isset function

Upvotes: 0

Awlad Liton
Awlad Liton

Reputation: 9351

You have posted form in same file. so you need to check if form is submitted or not.

Try like this:

 if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
    echo str_shuffle($_POST['value']);
    }

Upvotes: 3

Related Questions