栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过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。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/449078.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号