简体   繁体   中英

Why sharing data between two php files will be deleted?

I have a php form. User try to fill up the form. When one of the fields is missed I want to keep the data entered in other fields and ask them to only fill up the missing field. I started with two fields. I have 3 files. One is global.php which is saving data:

<?php

    $name;
    $name = "";

    global $email;
    $email = "";
?>

Second one is the form which I bring the part for name:

<?php include_once('global.php');?>
    <label>

        <span>Username:</span>

        <input id="name" type="text" name="username" value ="<strong><?php  echo $name;?></strong>" placeholder="Enter your user name" maxlength="20" />
    </label>

3rd is the php file which process the data and check for validation:

include_once('global.php'); 
if (empty($_POST["username"]) || strlen($_POST["username"]) > 30)
    {
        $GLOBALS['nameErr'] = "Name is required";
        header('Location: registration.php');
    }
else
    {
        $GLOBALS['name'] = test_input($_POST["username"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$name))
        {
            $GLOBALS['nameErr'] = "Only letters and white space allowed";
            header('Location: registration.php');            
        }
    }

When the user fill up the form and miss the email address and press register button it will delete the data already entered in the field of name. But name is global name and has been changed. I wondered when this has been changed? My second question is when I include a file, I initialize the variable in the included file again or they remain?

After Barif idea I changed it to:

<label>
    <span>Username:</span>
    <input id="name" type="text" name="username" value ="<?php echo "$_SESSION['name']  ;?>" placeholder="Enter your user name" maxlength="20" />
</label>

But it says "Undefined index: name"

Finally I found the problem. When I defined $_SESSION['name'] I was assigning it to "" so it was "" all the time.

$GLOBALS not storing data between HTTP-queries, you should use session for storing data between requests

include_once('global.php'); 
    if (empty($_POST["username"]) || strlen($_POST["username"]) > 30) {
        $_SESSION['nameErr'] = "Name is required";
        header('Location: registration.php');
    } else {
        $_SESSION['name'] = test_input($_POST["username"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
            $_SESSION['nameErr'] = "Only letters and white space allowed";
            header('Location: registration.php');            
        }
    }

Also you should start session session_start() in your registration.php

In your form you need check, if variable is defined:

<label>
    <span>Username:</span>
    <input id="name" type="text" name="username" value ="<?php echo isset($_SESSION['name']) : $_SESSION['name'] ? '';?>" placeholder="Enter your user name" maxlength="20" />
</label>

When you are viewing the form (your second php file) you are including globals.php again, which overrides $name value to empty string.

Regarding your second question, when you include a file you get all varibles defined there, you do not need to initialize them again.

Try this.

Instead of using globals etc...

Assuming you are submitting the form to the same file where the form is.

why not use the $_POST variable itself.

<input id="name" type="text" name="username" value ="<?php if(isset($_POST['username'])) echo $_POST['username']; ?>" placeholder="Enter your user name" maxlength="20" />

I'll site some example

addusername.php

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
         //DO QUERY HERE
         echo "You submitted something!<br/>"; 
}

?>
<form name='myForm' action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>
     Username: <input type='text' name='username' value='<?php if(isset($_POST['username'])) echo $_POST['username']; ?>' /><br/>
     Email: <input type='text' name='email' value='<?php if(isset($_POST['email'])) echo $_POST['email']; ?>' /> <br/>
     <input type='submit' value='Submit' />

</form>

Try it. Save it in a php file then try filling up 1 textfield then hit submit. The data of the other field is not lost. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM