以前,我使用php将表单数据附加到json文件中。目前,我正在处理php文件,而不是json文件,它有两个参数,如图图像url链接所示,它显示给用户。
我的表格是
<form action="process.php" method="POST">
Name:<br>
<input type="text" name="name">
<br><br/>
Download Url:<br>
<input type="text" name="downloadUrl">
<br><br>
<input type="submit" value="Submit">
</form>
我的php文件是
{"downloadurl":[
{"name":"भाद्र ६ : LCR Series",
"downloadurl":"https://drive.google.com/uc?export=download&id=1In76AN2Y5_qXV5ucXDXWx1PTKTTIvD3d"
},
{"name":"भाद्र ६ : LCR Parallel",
"downloadurl":"https://drive.google.com/uc?export=download&id=1R9Ia4X12JZMsTn_vF6z443K6wKI2Rfeu"
}
]}
如何使用追加的新数据,以便当单击submit按钮时,在上述php文件的基础上添加新数据,新文件将被添加到
{"downloadurl":[
{"name":"भाद्र ६ : New appended Data",
"downloadurl":"This is new Text added on Top"
},
{"name":"भाद्र ६ : LCR Series",
"downloadurl":"https://drive.google.com/uc?export=download&id=1In76AN2Y5_qXV5ucXDXWx1PTKTTIvD3d"
},
{"name":"भाद्र ६ : LCR Parallel",
"downloadurl":"https://drive.google.com/uc?export=download&id=1R9Ia4X12JZMsTn_vF6z443K6wKI2Rfeu"
}
]}
在顶部,它将显示给用户。
发布于 2018-09-28 14:51:17
从你的问题来看,我认为这将解决你的问题。我使用array_unshift()是因为您使用的短语on top使我认为您希望在现有数据之前显示数据,如果这不正确,请将array_unshift()替换为array_push(),以便它在数据之后添加,或者参见解决方案2。
解决方案1:
<?php
//This is where your JSON file is located
$jsonFile = '/path/to/json/file';
//Get the contents of your JSON file, and make it a useable array.
$JSONString = json_decode( file_get_contents( $jsonFile ), true );
//This is the new data that you want to add to your JSON
$newData = array(
//Your data goes here
);
//Add the new data to the start of your JSON
array_unshift($existingData, $newData);
//Encode the new array back to JSON
$newData = json_encode( $existingData, JSON_PRETTY_PRINT );
//Put the JSON back into the file
file_put_contents($jsonFile, $newData);
?>解决方案2:
<?php
//This is where your JSON file is located
$jsonFile = '/path/to/json/file';
//This is the new data that you want to add to your JSON
$newData = array(
//Your data goes here
);
//Encode the new data as JSON
$newData = json_encode( $newData, JSON_PRETTY_PRINT );
//append the new data to the existing data
file_put_contents( $jsonFile, $newData, FILE_APPEND );
?>发布于 2018-09-28 14:50:01
我不明白为什么要在PHP文件的末尾追加文本,但是可以使用file_put_contents()和FILE_APPEND标志。
https://stackoverflow.com/questions/52557730
复制相似问题