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

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测