douqi3195 2014-01-28 11:08
浏览 25
已采纳

AJAX不通过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

  • 写回答

2条回答 默认 最新

  • dstt1818 2014-01-28 13:02
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?