dongwei3172 2018-10-12 18:53
浏览 45
已采纳

动态数组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>
  • 写回答

2条回答 默认 最新

  • dongxin2734 2018-10-12 21:09
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 采用栈和深度优先算法对我的代码进行修改显示路径
  • ¥15 pointnet2包安装
  • ¥20 射频功率问题,解答者有酬谢!
  • ¥80 构建降雨和积水的预测模型
  • ¥15 #Qt Transform setTransform()在鼠标拖动移动视角是一致在原地不动,无法变换视角(细微观察似乎视图有在原地抖动),无法变换视角(细微观察似乎视图有在原地抖动)
  • ¥50 如何利用无人机拍摄的数码照片测量鸟卵的长短径
  • ¥100 github贡献者给与奖励
  • ¥15 使用DS18B20+ESP8266获取温度数据返回-127.00
  • ¥15 odbc代码新增sqlserver数据源
  • ¥15 求一个USB指纹识别的驱动