0

I am trying run a small PHP script. I get an error when I try to run this small PHP line: <?PHP print $firstname ; ?> within the html code. According to my tutorial guide this line in the value element should ensure that I keep any input text entered into the text fields even after I refresh the browser, but it does not! the following error message occurs:

Notice: Undefined variable: surname in C:\wamp\www\pages\basicForm.php on line 27 Call Stack #TimeMemoryFunctionLocation 10.0007366504{main}( )..\basicForm.php:0 " NAME="surname">

Why?

and the code:

<!DOCTYPE html>
<html>
<head>
    <title></title>
        <?PHP
        if (!empty($_POST)) {
            $firstname = $_POST['firstname'];
            $surname = $_POST['surname'];
            print($firstname);
            print($surname);
        }
        else {

        } print( 'welcomes');
    ?>
</head>
<body>
<Form name ="form1" Method ="POST" Action ="basicForm.php">
    <label>


    <INPUT TYPE = 'TEXT' Name ='firstname' VALUE="<?PHP print $firstname ; ?>">
    </label>

    <p>
        <label>
            <INPUT TYPE="TEXT" VALUE="<?PHP print($surname); ?>" NAME="surname">
        </label>

    <p>
        <Input Type = "Submit" Name = "Submit1" Value = "Login">
</FORM>


</body>
</html>
afuzzyllama
  • 6,538
  • 5
  • 47
  • 64

4 Answers4

0

All you need to assign initial value as below

$firstname = '';
$surname = '';

Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globals turned on. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array. isset() language construct can be used to detect if a variable has been already initialized.

Read more in detail: PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"

Community
  • 1
  • 1
GBD
  • 15,847
  • 2
  • 46
  • 50
  • Yes That works - I just declared the variables as you said in the PHP code at the top before the html code is executed. Thanks.p.s I wish my tutorial guide could of better informed me. Mined you I could of spotted mistake my I guess. – Leigh Pierce Dec 27 '12 at 18:16
0

$surname isn't set at first:

<INPUT TYPE="TEXT" VALUE="<?PHP print($surname);

It's only getting set at the top if $_POST is NOT empty.

Paul Dessert
  • 6,363
  • 8
  • 47
  • 74
0

if the $_POST variable is empty then $surname will not be created. There are a couple of ways that you can get around this.

1, predefine your variables:

$firstname = '';
$surname = '';
if (!empty($_POST)) {
    $firstname = $_POST['firstname'];
    $surname = $_POST['surname'];
}else {

}

2, Use ternary syntax:

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

Both will ensure that your variables are created before printing them.

Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
0

According to my tutorial guide this line in the value element should ensure that I keep any input text entered into the text fields even after I refresh the browser

It seems you misunderstood your tutorial.
Server side technology can't help until you actually send something to the server.
So, it seems you're just entering your values and refreshing your page before submit. Although some browsers may preserve entered values by their own will, but server side PHP has nothing to do with it.

Another story if you populate your form with values that's already on the server - such filled form will persists even refreshes.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345