每次按下“提交”按钮时,都会在.text文件上换行

时间:2014-12-17 21:42:47

标签: php html parameters text-files

我希望我的代码将数据保存到文本文件中。我得到了这个:

Name: Geo
Address: California
Phone Number: 661212121
# of Boxes: 1
Scheduled Date: Tues

但是,每当我再次使用HTML中的新数据点击提交按钮时,它就会替换文本文件中的旧数据。

Name: Paul
Address: Sweden
Phone Number: 1234
# of Boxes: 5
Scheduled Date: Wed

我想让输出看起来像:

Name: Geo
Address: California
Phone Number: 661212121
# of Boxes: 1
Scheduled Date: Tues

Name: Paul
Address: Sweden
Phone Number: 1234
# of Boxes: 5
Scheduled Date: Wed

我的HTML:

     <form action='testing.php' method='GET'>
    Name: <br/>
    <input type='text' name='name'/>
    <br/>
    Address: <br/>
    <input type='text' name='address'/>
    <br/>
    Contact Number: <br/>
    <input type='text' name='phone'/>
    <br/>
    # of Boxes: <br/>
    <input type='text' name='box'/>
    <br/>
    Scheduled Date <br/>
    <input type='text' name='date'/>
    <br/>
    <input type='submit' value='SUBMIT'/>
</form>

PHP:

<?php
$name = $_GET['name'];
$address = $_GET['address'];
$phone = $_GET['phone'];
$box = $_GET['box'];
$date = $_GET['date'];

$file_handle = fopen("pickup.txt", "w");
$file_contents = "Name:" . $name . "\r\n" . "Address:" . $address . "\r\n" . "Phone Number:" . $phone . "\r\n" . "# of Boxes:" . $box . "\r\n" . "Scheduled Date:" . $date;

fwrite($file_handle, $file_contents);
fclose($file_handle);
print "file created and written to pickups.txt";

2 个答案:

答案 0 :(得分:1)

使用追加选项 -

$file_handle = fopen("pickup.txt", "a"); // replaced 'w'

请参阅fopen modes

  

&#39;一个&#39;仅供写作;将文件指针放在文件的末尾。如果该文件不存在,请尝试创建它。

答案 1 :(得分:0)

尝试

$file_handle = fopen("pickup.txt", "a");

一起
$file_contents = "Name:" . $name . "\r\n" . "Address:" . $address . "\r\n" . "Phone Number:" .      $phone . "\r\n" . "# of Boxes:" . $box . "\r\n" . "Scheduled Date:" . $date . "\r\n";