duanlu6268 2017-10-12 06:24
浏览 39
已采纳

如何在数组php文件中添加值当在HTML中提交html表单时?

I have one html form as index.php and also another mydata.php file. I want to put data into mydata.php file, but there is some issue

index.php

<form action="" method="POST">
    <input name="field1" type="text" />
    <input name="field2" type="text" />
    <input type="submit" name="submit" value="Save Data">
</form>

<?php
if (isset($_POST['field1']) && isset($_POST['field2'])) {
    if (!filesize('mydata.php')) {
        $data0 = '<?php $a = array(' . "
";
        $ret = file_put_contents('mydata.php', $data0, FILE_APPEND | LOCK_EX);
    }

    $data = '"' . $_POST['field1'] . '"' . '=>' . '"' . $_POST['field2'] . '",' . "
";
    $ret = file_put_contents('mydata.php', $data, FILE_APPEND | LOCK_EX);

    if ($ret === false) {
        die('There was an error writing this file');
    } else {
        echo "$ret bytes written to file";
    }
}
?>

mydata.php

$array = array("a"=>"b");

When I add submit new value i want to need push new array like my post data

   Array
(
    [field1] => c
    [field2] => d
    [submit] => Save Data
)

$array = array("a"=>"b","c"=>"d");
  • 写回答

2条回答 默认 最新

  • doulin6088 2017-10-12 06:56
    关注

    You just need to add data to $array, generate the PHP code and then save it into file.

    Example with PHP file:

    <?php
    // data for the form ($_POST), all data from the client (browser) MUST be validated and sanitized
    $formData = [
        'field1' => 'c',
        'field2' => 'd'
    ];
    
    // load mydata.php if it was not loaded before
    require_once 'mydata.php';
    
    // add new data or update existen
    $array = array_merge($array, $formData);
    $tab = '    ';
    
    // generate the new content for the mydata.php file
    $newContent = '<?php' . PHP_EOL . '$array = [' . PHP_EOL;
    foreach ($array as $key => $value)
        $newContent .= $tab . "'$key' => '" . addslashes($value) . "'," . PHP_EOL;
    
    $newContent .= '];' . PHP_EOL;
    
    //save the new content into file
    file_put_contents('mydata.php', $newContent);
    

    But I really recommend to you use the JSON file for that.

    Example with JSON file:

    <?php
    // data for the form ($_POST), all data from the client (browser) MUST be validated and sanitized
    $formData = [
        'field2' => 'c',
        'field3' => 'd'
    ];
    
    $array = [];
    
    // load data
    if (file_exists('mydata.json'))
        $array = json_decode(file_get_contents('mydata.json'), true);
    
    // add new data or update the existen
    $array = array_merge($array, $formData);
    
    // save the new data into file
    file_put_contents('mydata.json', json_encode($array), LOCK_EX);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆