动态数组php(在relod php表单后将数据附加到数组)

问题描述:

i want to get data from html form and add append into multidimensional array ; but after submit the data will be changed.(my Previous has been remove.)

<body>
<?php
$name2=$family2=$email2=$age2 ="";
$arr2demin = array();
if($_SERVER["REQUEST_METHOD"]=='POST'){
$name2 = $_POST["name"];
$family2 = $_POST["family"];
$email2 = $_POST["email"];
$age2= $_POST["age"];


}

        
?>
    <form name="myform" id="cls-frm" action="" method="post"  >
<!-- form inputs-->
        </form>

        
        <?php
        $arr=array("$name2","$family2","$email2","$age2");
array_push($arr2demin,$arr);
echo "<h1>your input is :</h1> <br>";
       
print_r($arr2demin);


?>
    
    </body>

</div>

If you're looking to 'store' the form-data (records) you need a persistent storage mechanism (file/database). However, you can work on your data in a session, see the records that are 'submitted' during a session, stored in a session variable (array).

<?php
session_start();
$arr = []; //initialize empty array
$name2 = $family2 = $email2 = $age2 = "";
?>

<!doctype html>
<html>
<body>
<form name="myform" id="cls-frm" action="" method="post">
    <div>
        <label for="name">Enter your first name: </label>
        <input type="text" name="name" required>
    </div>
    <div>
        <label for="name">Enter your family name: </label>
        <input type="text" name="family" required>
    </div>
    <div>
        <label for="name">Enter your email: </label>
        <input type="email" name="email" required>
    </div>
    <div>
        <label for="name">Enter your age: </label>
        <input type="text" name="age" required>
    </div>
    <div>
        <input type="submit" name="submit" value="submit">
    </div>
</form>


<form name="myform2" id="cls-frm2" action="" method="post">
    <div>
        <label for="name">clear all my records currently in session array</label>
        <input type="submit" name="clear" value="clear data">
    </div>
</form>

<?php
/* clear the data in session array */
if (isset($_POST['clear'])) {
    $_SESSION = [];
}

if (isset($_POST['submit'])) {
    $name2 = $_POST["name"];
    $family2 = $_POST["family"];
    $email2 = $_POST["email"];
    $age2 = $_POST["age"];

    $arr = ["$name2", "$family2", "$email2", "$age2"];
    $_SESSION['arr2demin'][] = $arr; // create a new sub-array for each set of data

    echo "<h1>my current session records:</h1> <br>";
    echo '<pre>';
    print_r($_SESSION['arr2demin']);
    echo '</pre>';
    echo 'NOTE: if you want to store your session data persistently, you need to save session records to file/database...';
}
// database / file storage code here...
?>
</body>
</html>

The problem is that each request doesn't know anything about state of previous requests.

    $arr2demin = array();

At every request script creates new array. You need to store previous values inside your form input using function that will convert array to a string:

    <input type="hidden" name="arr2demin" value="<?php echo serilize($arr2demin);?>" />

But adding new array item is going after form rendering so you will need to move it before form output:

    //is post condition
    ...
    $arr=array("$name2","$family2","$email2","$age2");
    array_push($arr2demin,$arr);
    ?>
    <form
    ...

And after form was submitted with values retrieve value and restore to array:

   if($_SERVER["REQUEST_METHOD"]=='POST'){
   ...
   $arr2demin = unserialize($_POST["arr2demin"]);
   ...