通过AJAX将数据从localStorage发送到PHP并将其保存在HTML文件中

通过AJAX将数据从localStorage发送到PHP并将其保存在HTML文件中,第1张

通过AJAX将数据从localStorage发送到PHP并将其保存在HTML文件

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_enpre($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>

注意: 如果要使用HTML格式的JSON数据,请在PHP代码中将your_data文件的文件格式替换为html。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5174255.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-18
下一篇 2022-11-18

发表评论

登录后才能评论

评论列表(0条)

保存