簡體   English   中英

如何使用php將輸入數據存儲到文本文件並從文本文件獲取數據?

[英]How to store input data using php into text file and get data from text file?

我將當前輸入數據存儲在文本文件中,並從同一文件中獲取數據。 例如:在form.php中,我輸入了表單:

<form name="formData" method="POST" action="landing.php">
<p>NEW DATA:</p>
<input type="text" id="new" name="newdata" />
<p>CURRENT DATA:</p>
<input type="text" id="current" name="current" />
<input type="submit" name="Invia" Value="Invia i dati">
<input type="reset" id="reset" name="Reset" Value="Reset">
</form>

landing.php中,我有以下內容:

<?php
$newdata = $_POST['newdata'];
$current = $_POST['current'];
$file = 'box.xml';
$xml = file_get_contents($file);
$output  = str_replace($current, $newdata, $xml);
file_put_contents($file, $output);
?>

如您所見,提交當前數據和新數據后,xml文件將與新數據一起保存。

這是我的問題:

提交后,如何在文本文件中存儲$ newdata ,它將在form.php中顯示為$ current

在form.php上添加此代碼

  $file = 'box.xml';
  $xml = file_get_contents($file);

之后,在CURRENT DATA下,添加

  <input type="text" id="current" name="current" value="<?php echo $xml; ?>" />

在Landing.php上編輯

   <?php
     $newdata = $_POST['newdata'];
     $file = 'box.xml';
     /* there is no need of this code
     $current = $_POST['current'];
     $xml = file_get_contents($file);
     $output  = str_replace($current, $newdata, $xml);*/
     file_put_contents($file, $newdata);
    ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM