如何使用PHP从Web界面中的表单写入现有CSV文件?

时间:2017-03-10 23:12:52

标签: php html csv

我正在尝试使用我网站上的表单写入CSV文件,但它没有写任何内容,只是让我上传另一个CSV文件。

我的代码:

表单代码:

<form action="" method="post">
    <fieldset style="float:left; text-align: left">

        <legend>Add Users</legend>
        <label for="fn">
            First Name: <input type="text" id="fn" name="fn" onkeypress="fill()" required="required" placeholder="First Name" size="20"/>
        </label>
        <label for="ln" >
            Last Name: <input type="text" id="ln" name="ln" onkeyup="fill()" required="required" placeholder="Last Name" size="20"/>
        </label>

        <br/>

        <label for="full">
            Full Name: <input type="box" id="full" name="full" required="required" placeholder="Full Name" size="54"/>
        </label>

        <br/>

        <label for="log">
            User Logon: <input type="text" id="log" name="log" onkeypress="fill2()" required="required" placeholder="User Logon" size="27"/>
        </label>
        <select>
            <option value="test">
                test
            </option>
        </select>

        <br/>

        <label for="log2">
            User logon name (pre-Windows 2003): <select>
            <option value="test\">
                test\
            </option>
        </select><input type="text" id="log2" name="log2" required="required" placeholder="Username" size="20"/>
    </label>

    <br/>

    <label for="pwd">
        Password: <input type="password" id="pwd" name="pwd" required="required" placeholder="Password" size="20"/>
    </label>
    <label for="cpwd">
        Confirm Password: <input type="password" id="pwd" name="cpwd" required="required" placeholder="Password" size="20"/>
    </label>
    <p for="1st" style="font-size: 16px"><input type="checkbox" id="1st" name="1st" value="1st"/>User must change password at next logon</p>
    <p for="2nd" style="font-size: 16px"><input type="checkbox" id="2nd" name="2nd" value="2nd"/>User cannot change password</p>
    <p for="3rd" style="font-size: 16px"><input type="checkbox" id="3rd" name="3rd" value="3rd"/>Password never expires</p>
    <p for="4th" style="font-size: 16px"><input type="checkbox" id="4th" name="4th" value="4th"/>Account is disabled</p>

    <input type='submit' name='submit' value='Add User'>
    <button type="reset" value="Clear" onclick="" id="auser">Clear</button>

</fieldset>
</form>

php代码:

if(isset($_POST['submit']))
{
    //collect form data
    $fn = $_POST['fn'];
    $ln = $_POST['ln'];
    $full = $_POST['full'];
    $log = $_POST['log'];
    $log2 = $_POST['log2'];
    $pwd = $_POST['pwd'];

    //if no errors carry on
    if(!isset($error))
    {
        # Title of the CSV
        $title   = "First Name, Last Name, Full Name, User Logon, User Logon(pre-2003), Password,\n";
        $space = "\n";
        //set the data of the CSV
        $Content = "$fn, $ln, $full, $log, $log2, $pwd\n";

        # set the file name and create CSV file
        $FileName = ("adduser.csv");
        header('Content-Type: application/csv'); 
        header('Content-Disposition: attachment; filename="' . $FileName . '"'); 
        echo $title;
        echo $space;
        echo $Content;
        exit();
    }
}

我需要的是,当我在表单上按添加用户时,它会将该用户添加到adduser.csv文件中,但是现在就像我说的那样,让我下载一个CSV文件,而不是写到adduser.csv文件。

1 个答案:

答案 0 :(得分:0)

您可以使用feature标记file_put_contents将数据附加到文件中。

FILE_APPEND