简体   繁体   中英

AJAX not posting data to text file via PHP

I have a form and I want to save the entries in a text file on the server when a submission is done.

The jquery I'm using is:

$(document).ready(function(){
localStorage.clear();

$("form").on("submit", function() {
    if(window.localStorage!==undefined) {
        var fields = $(this).serialize();

        localStorage.setItem("eloqua-fields", JSON.stringify( fields ));
        alert("Stored Succesfully");
        $(this).find("input[type=text]").val("");
        alert("Now Passing stored data to Server through AJAX jQuery");
        $.ajax({
           type: "POST",
           url: "backend.php",
           contentType: 'application/json; charset=utf-8',
           data: {data: fields},
           success: function(data) {
              $('#output').html(data);
           }
        });
    } else {
        alert("Storage Failed. Try refreshing");
    }
});
});

And the PHP I'm using is:

 <h1>Below is the data retrieved from SERVER</h1>
<?php
    date_default_timezone_set('America/Chicago'); // CDT
    echo '<h2>Server Timezone : ' . date_default_timezone_get() . '</h2>';
    $current_date = date('d/m/Y == H:i:s ');
    print "<h2>Server Time : " . $current_date . "</h2>";
    $dataObject = $_POST['data'];
    $json = json_decode($dataObject);
    echo $json;
    file_put_contents('your_data.txt', $json);
?>

You can see the live version of the form here: http://hackingarticles.com/marketer/

The problem is that my code is not posting any data to http://hackingarticles.com/marketer/your_data.txt

I've been trying to solve this for so long but it's just not working for me.

Any help is appreciated.

Cheers

PHP:

<h1>Below is the data retrieved from SERVER</h1>
<?php
        date_default_timezone_set('America/Chicago'); // CDT
        echo '<h2>Server Timezone : ' . date_default_timezone_get() . '</h2>';
        $current_date = date('d/m/Y == H:i:s ');
        print "<h2>Server Time : " . $current_date . "</h2>";

        $dataObject = $_POST; //Fetching all posts

        echo "<pre>"; //making the dump look nice in html.
        var_dump($dataObject);
        echo "</pre>";

            //Writes it as json to the file, you can transform it any way you want
        $json = json_encode($dataObject);
        file_put_contents('your_data.txt', $json);
?>

JS:

<script type="text/javascript">
$(document).ready(function(){
    localStorage.clear();

    $("form").on("submit", function() {
        if(window.localStorage!==undefined) {
            var fields = $(this).serialize();

            localStorage.setItem("eloqua-fields", JSON.stringify( fields ));
            alert("Stored Succesfully");
            $(this).find("input[type=text]").val("");
            alert("Now Passing stored data to Server through AJAX jQuery");
            $.ajax({
               type: "POST",
               url: "backend.php",         
               data: fields,
               success: function(data) {
                  $('#output').html(data);
               }
            });
        } else {
            alert("Storage Failed. Try refreshing");
        }
    });
});
</script>

Try setting the contentType property in the ajax request, like, contentType: 'application/json; charset=utf-8' contentType: 'application/json; charset=utf-8' . I already have been through this problem (not sending any data to the server) and that solved my problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM