dsbj66959 2016-03-06 20:56
浏览 53
已采纳

将HTML DOM保存到服务器上的文件中

I have an html5 web page that allows users to drag-n-drop objects between divs. After a user has moved objects around, I would like to save the current DOM to a file on my web server.

I know I can get the current HTML DOM using javascript but of course, I cannot save to a file on my server using javascript. So I thought about passing the html to a PHP page to do the "save" function, but I cannot figure out how to get the html passed to a PHP page. I've tried sending it as an argument in the URL with URI encoding, but the PHP page is not properly getting the entire string from the URL.

Should this approach work? If so, what am I missing to get the html string passed correctly to a PHP page? Or should I be using some other method?

  • 写回答

2条回答 默认 最新

  • doulin2555 2016-03-06 21:42
    关注

    ajax is the way to go here. If you are not familiar with ajax, please google it and learn it well. Any modern web app needs to have ajax integration in some way.

    Here is how you can use javascript to communicate with the server. Please Note I'm using JQuery

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $(".draggableDivs").mouseup(function(event){
            var documentStructure = ''; // whatever js you use to get document structure
            var d = {"document_structure": documentStructure};
            $.ajax({
                url: "test.php", //Your url both relative and fixed path will work
                type: "POST", // you need post not get because you are sending a lot of data
                data: d,
                success: function(response) {
                    alert('saved');
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });
        });
    });
    </script>
    

    on the server you would then do your php and save the data. After you are done you can just respond with a json object if needed, if not just exit

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度