将插入保存到文件

时间:2016-11-25 12:52:55

标签: javascript url-rewriting fwrite

我有一个输入表单,想要保存我已插入文件的数据。我应该使用get并为此设置吗?任何人对我应该做什么都有任何想法?



function myfunction() {
    if (validation()) // Calling Validation Function
    {
        // Serializing Form Data And Displaying It In <p id="wrapper"></p>
        document.getElementById("wrapper").innerHTML = serialize(document.forms[0]); // Serialize Form Data
        document.getElementById("form").reset(); // Reset Form Fields
    }
}

function validation() {
    var name = document.getElementById("namn").value;
    var number = document.getElementById("number").value;
}
&#13;
<!DOCTYPE html>
<html lang="sv">
    <head>
        <title>Write</title>
        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="style.css">
        <script src="http://form-serialize.googlecode.com/svn/trunk/serialize-0.2.min.js" type="text/javascript"></script> <!-- For Serialization Function -->
        <script src="hej.js"></script> <!-- Include JavaScript File Here-->
    </head>
    <body>

        <div id="main">
            <div id="login">
                <hr/>
                <form action="" method="post">
                    <label>Name:</label>
                    <input type="text" name="name" id="name" required="required" placeholder="fill in your name"/><br /><br />
                    <label>Number :</label>
                    <input type="text" name="number" id="number" required="required" placeholder="0876856"/><br/><br />
                    <input onclick="myfunction()" type="submit"id= "submit" value=" Submit " name="submit"/><br />
                </form>
            </div>
        </div>
    </body>
</html>
&#13;
&#13;
&#13;

提前致谢!

1 个答案:

答案 0 :(得分:0)

如果您使用普通$_POST,则可以通过$ _POST访问数据,并将其与fopenfwrite一起写入文件。通过ajax,您必须将数据发送到PHP文件并在那里执行相同的操作。您无法通过Javascript访问本地文件系统。

<?php
if(isset($_POST) && !empty($_POST){

// reformat your data here and format it
$yourDataToWriteInTheData = $_POST['firstname'] . '|' . $_POST['lastname'] . PHP_EOF;
// Open the file (or create it, read the fopen manual)
$fileHandler = fopen('file1.txt', 'a+');
fwrite($fileHandler, $yourDataToWriteInTheData);
fclose($fileHandler);
}