Saving form output to a text file by sending jQuery / Javascript to php file?

I have a simple html form which, as you can see below, has been pre-populated with information, for example, for purposes.

Example form

Currently, when you click the button, the form is saved in Google Docs, which work great.

However, I also want it to write / save the form output to a text file on the server on which the page will be hosted.

An example of a pre-filled form: http://jsfiddle.net/owcnwfhp/

The format that I want it to be recorded / saved as shown below, each new line is a new representation of the form.

An example of the expected result:

3/18/2015 8:06:27   testname    test4   test2   test3   test4   test5   test6   test7   test8   test9   test10
3/18/2015 8:07:07   testname1   test4   test4   test3   test4   test5   test6   test7   test8   test9   test10
3/18/2015 8:08:01   testname2   test2   test2   test3   test4   test5   test6   test7   test8   test9   test10
3/18/2015 8:09:41   testname3   test2   test5   test3   test4   test1   test6   test7   test8   test9   test10

The separator character must be an ASCII tab character

/ , , , , /.

, // , Google.

function breakRedirect() {
    $('#container').replaceWith('<div id="complete">Completed</div>');
};

...

, , (, javascript/jquery , ), , PHP ? ...

function breakRedirect() {

    var month = new Array();
        month[0] = "01"; month[1] = "02"; month[2] = "03"; month[3] = "04"; month[4] = "05"; month[5] = "06"; month[6] = "07"; month[7] = "08"; month[8] = "09"; month[9] = "10"; month[10] = "11"; month[11] = "12";
    var collection = new Date().getDate() + "/" + month[new Date().getMonth()] + "/" + new Date().getFullYear() + " "+ new Date().getHours() + ":" + new Date().getMinutes() + ":" + new Date().getSeconds();

    var $inputs = $("input")
    $inputs.each(function () {
        var value = this.value;
        if (value != "Submit Selections" && value != "Clear Selections") {
            collection += "\t" + value
        };
    });

    var $selects = $("select")
    $selects.each(function () {
        var value = this.value;
        collection += "\t" + value
    });

    console.log(collection);

    $('#container').replaceWith('<div id="complete">Completed</div>');
};

: http://jsfiddle.net/owcnwfhp/1/

?

+4
1

JQuery

$. Ajax()


, jQuery.ajax. . .

$.ajax({
      method: "POST",
      url: "some.php",
      data: { name: "John", location: "Boston" }
    })
      .done(function( msg ) {
        alert( "Data Saved: " + msg );
      });

:

data: $('form#myForm').serialize(),

: jQuery website -.ajax(), fooobar.com/questions/19383/...

PHP

file_get_contents() $_POST []


PHP $_POST .

// WARNING! You should do some valiadation before writing to the file!
<?php
   $name = $_POST["name"];
   $location = $_POST["location"];
   $new_line = $name . "your tab ascii code" . $location . "\n";
   $file = 'people.txt';

   // Open the file to get existing content
      $current = file_get_contents($file);

   // Append a new person to the file
      $current .= $new_line;

   // Write the contents back to the file
      file_put_contents($file, $current);
?>

: PHP -

+1

All Articles