簡體   English   中英

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

[英]Better Method to Create a file using Innert html

在這個項目中,當用戶提交論壇時,我將創建一個新文件。 該文件包含#main 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>

我為此目的使用phpJquery

但是這里的問題是#main div包含太多內部html。

所以當作為$_POST傳遞時將是一個問題?

$_POST是否會限制該值(如果超過某個值)?

是否有其他替代方法或好的方法來解決此問題?

您可以考慮采用的一種方法,而不是實際嘗試發布大量數據,而只是將有問題的文檔/頁面的POST發布到PHP腳本,然后可以使用dom操作為您找到所需的內容。 這樣應該可以使請求變得非常快,並且不會對POST數據造成任何限制。

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM