Javascript 使用Innert html创建文件的更好方法

Javascript 使用Innert html创建文件的更好方法,javascript,php,jquery,html,Javascript,Php,Jquery,Html,在这个项目中,我在用户提交论坛时创建了一个新文件。该文件包含主div中的html结构。请看我的密码 <?php if(ISSET($_POST['submit'])){ $myfile = fopen($_POST['user_name'].".html", "w") or die("Unable to open file!"); $txt = $_POST['inner_html']; fwrite($myfile, $txt); fclose($myf

在这个项目中,我在用户提交论坛时创建了一个新文件。该文件包含主div中的html结构。请看我的密码

<?php 
if(ISSET($_POST['submit'])){
    $myfile = fopen($_POST['user_name'].".html", "w") or die("Unable to open file!");
    $txt = $_POST['inner_html'];
    fwrite($myfile, $txt);
    fclose($myfile);

}

?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">
   <div class="new-classs" style="color:green;width:100px">
          <img src="tst.jpg" />
          <span>Content of #main div goes here</span>
   </div>     
</div>

<form method="post" action="">
    <input type="text" name="user_name" class="user_name" required>
    <input type="hidden" name="inner_html" class="inner_html">
    <input type="submit" value="submit" name="submit" class="submit">
</form>

<script>
 $('.submit').on("click",function(e){
  $(".inner_html").val($("#main").html());

});
</script>
为此,我使用php和Jquery

但这里的问题是,有时主div包含太多的内部html

因此,当作为$邮政传递时会出现问题吗

$\如果超过某个值,POST将限制该值


有没有其他或好的方法来解决这个问题

> P>一种方法,而不是实际尝试发布大量数据,只需将文档/页的URL发布到PHP脚本,然后可以使用DOM操作来为您找到所需内容。这将使请求非常快速,并且不会在发布的数据方面遇到任何限制

ajax函数可以很容易地被jQuery版本取代——我不使用jQuery

表单中的按钮不再是传统意义上的表单提交,而是触发一个ajax请求,将页面和用户名细节发送到后端php代码

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['username'], $_POST['page'] ) && !empty( $_POST['page'] ) ){
        ob_clean();

        $username=filter_input( INPUT_POST, 'username', FILTER_SANITIZE_STRING );

        /* change path to suit environment */
        $targetfile='c:/temp/'.$username.'.html';




        libxml_use_internal_errors( true );

        /* Load the current or selected page into a new DOMDocument */
        $dom=new DOMDocument;
        $dom->validateOnParse=false;
        $dom->standalone=true;
        $dom->strictErrorChecking=false;
        $dom->recover=true;
        $dom->loadHTMLFile( $_POST['page'] );

        /* Find the element in the DOM to save */
        $div = $dom->getElementById('main');

        /* Save copy to this Document */
        $doc=new DOMDocument;

        /* Create a cloned copy of the DIV */
        $clone=$div->cloneNode( true );

        /* Add the clone to our new document */
        $doc->appendChild( $doc->importNode( $clone, true ) );

        /* write the cloned node innerHTML to file */
        $bytes = file_put_contents( $targetfile, $doc->saveHTML() );

        libxml_clear_errors();

        $dom = $doc = null;

        /* send some feedback to the client */
        exit( 'Bytes written: '.$bytes.' to '.$targetfile );
    }
?>
<!doctype html>
<html>
    <head>
        <title>Create File via AJAX & DOMDocument</title>
        <script type='text/javascript'>

            /* simple ajax function for post & get requests */
            function ajax(m,u,p,c,o){
                /*method,url,params,callback,options*/
                var xhr=new XMLHttpRequest();
                xhr.onreadystatechange=function(){
                    if( xhr.readyState==4 && xhr.status==200 )c.call( this, xhr.response, o, xhr.getAllResponseHeaders() );
                };

                var params=[];
                for( var n in p )params.push(n+'='+p[n]);

                switch( m.toLowerCase() ){
                    case 'post': p=params.join('&'); break;
                    case 'get': u+='?'+params.join('&'); p=null; break;
                }

                xhr.open( m.toUpperCase(), u, true );
                xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                xhr.send( p );
            }


            /* Add event listener to the button */
            document.addEventListener('DOMContentLoaded',function(e){
                document.getElementById('btn').addEventListener('click',function(e){
                    var username=document.getElementById('usr').value;
                    if( username!='' ){
                        ajax.call( this, 'post', location.href, { page:location.href, username:username }, function(r){
                            alert(r)
                        }, null );
                    }
                },false);
            },false);
        </script>
    </head>
    <body>
        <div id='main'>
           <div class='new-classs' style='color:green;width:100px'>
                  <img src='/images/tarpit.png' />
                  <span>Content of #main div goes here</span>
                  <span>Content of #main div goes here</span>
                  <span>Content of #main div goes here</span>
                  <span>Content of #main div goes here</span>
                  <span>Content of #main div goes here</span>
                  <span>Content of #main div goes here</span>
                  <span>Content of #main div goes here</span>
                  <span>Content of #main div goes here</span>
                  <span>Content of #main div goes here</span>
                  <span>Content of #main div goes here</span>
                  <h2>more content...</h2>
                  <!-- lots and lots of contents -->
           </div>     
        </div>
        <form method='post'>
            <input id='usr' type='text' name='user_name' class='user_name' required>
            <input id='btn' type='button' value='submit' name='submit' class='submit'>
        </form>
    </body>
</html>

你为什么撤销我的编辑?你能再编辑一个吗。因为我减少了代码之间的空间,所以请求的大小限制不取决于服务器配置吗?我怀疑是否有一个标准,根据您的服务器,您应该能够将其配置为允许更大的请求