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条)

报告相同问题?

悬赏问题

  • ¥15 数据库获取信息反馈出错,直接查询了ref字段并且还使用了User文档的_id而不是自己的
  • ¥15 将安全信息用到以下对象时发生以下错误:c:dumpstack.log.tmp 另一个程序正在使用此文件,因此无法访问
  • ¥15 速度位置规划实现精确定位的问题
  • ¥15 代码问题:df = pd.read_excel('c:\User\18343\Desktop\wpsdata.xlxs')路径读不到
  • ¥15 为什么视频算法现在全是动作识别?
  • ¥15 编写一段matlab代码
  • ¥15 用Python做岩石类别鉴定软件
  • ¥15 关于调取、提交更新数据库记录的问题
  • ¥15 之前删了盘从下vs2022遇见这个问题 搞了一整天了
  • ¥15 从Freecad中宏下载的DesignSPHysics,出现如下问题是什么原因导致的(语言-python)