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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀