0

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

3
  • 2
    The @ operator in PHP supresses warnings. See the manual Commented Feb 27, 2014 at 7:14
  • 1
    Who the hell is a Down Voter Has down voted every answer without a single comment about it :( Commented Feb 27, 2014 at 7:22
  • Sidenote: You're missing an = sign in action"" so that won't work. Commented Feb 27, 2014 at 15:31

3 Answers 3

2

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'];
Sign up to request clarification or add additional context in comments.

Comments

1

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'];
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.