Aysha Azura
Aysha Azura

Reputation: 179

Undefined variable error when the variable name is used in form filed

while executing my code some times a warning message comes like use of undefined variable even if the variable name is being used properly in the form field.

<form method="post" id="" action"">
<input type="text" name="name" />
<input type="submit" name="submit" value="submit">
</form>

$name=$_POST['name'];

even if the name is used in the form field warning message comes and i had to put @ in front of it

if(isset($_POST['submit']))
{

@$name=$_POST['name'];
}

why is is so can anyone help me out with it

Upvotes: 0

Views: 80

Answers (3)

GajendraSinghParihar
GajendraSinghParihar

Reputation: 9131

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

Upvotes: 0

Rakesh Sharma
Rakesh Sharma

Reputation: 13728

the reason was you have not initialized $name variable and also $_POST['name'] not found until your form not submitted

so try to initialize variable first like

$name = '';

or

$name= (isset($_POST['name']) ? $_POST['name'] : '');

or

if(isset($_POST['name']))
        $name=$_POST['name'];

Upvotes: 2

You are not at all submitting your <form> using a submit button. and the action attribute has no =

Should be like ...

Your HTML form

<form method="post" action="test.php">
<input type="text" name="name" />
<input type="submit" name="submit" />
</form>

test.php

if(isset($_POST['name']))
{
 echo $_POST['name'];
}

Upvotes: 1

Related Questions