如何在 PHP 请求中使用内联 CSS 和 jQuery 发送 HTML 数据?

How to send HTML data with inline CSS and jQuery in a PHP request?

我正在做一个项目,我正在服务器上创建一个新的 HTML 页面,方法是发送带有内联 CSS 作为请求变量的参数的 HTML。

问题是,如果遇到像#这样的符号,像&&&或HTML这样的符号代码,如  < >.


代码如下:

page.htm:

    <!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script>
            function creathtmldoc()
            {
                var data = document.getElementsByTagName('html')[0].innerHTML;
                var xmlhttp;
                if (window.XMLHttpRequest)
                {
                    xmlhttp = new XMLHttpRequest();
                }
                else
                {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.onreadystatechange = function()
                {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
                    {
                        alert("copy created");
                        alret(xmlhttp.responseText);
                        document.getElementById("display").innerHTML = xmlhttp.responseText;
                    }
                }
                xmlhttp.open("POST", "./creatpage.php?data="+data, true);
                xmlhttp.send();


            }
        </script>
    </head>
    <body>
        <div id="display">
            <div id="mydiv" style="border:2px solid #dddddd;margin: 0 auto;padding:70px;width:660px;height:900px;">
                <div style="clear:both;">
                    <div style="float:left;">Hello <strong>Gaurav</strong></div>
                    <div style="float:right;"><strong>&lt;@Your name&gt;</strong></div>
                </div>
                <div style="clear:both;">
                    <div style="float:left;">We are : <span style="font-family:courier new,courier,monospace"><em><strong>&lt;@Company name&gt;</strong></em></span></div>
                    <div style="float:right;"><em><strong><span style="font-family:courier new,courier,monospace">&lt;@Your designation&gt;</span></strong></em></div>
                </div>
                <div style="clear:both;">
                    <div style="float:left;"><span style="font-family:courier new,courier,monospace"><em><strong>&lt;@Company name with full address of company&gt;</strong></em></span></div>
                    <div style="float:right;"><span style="font-family:courier new,courier,monospace"><em><strong>&lt;@Your contact&gt;</strong></em></span></div>
                </div>
                <div style="clear:both;">&nbsp;</div>
                <div style="clear:both;">
                    <hr />
                    <div>We call you to inform about : <span style="font-family:courier new,courier,monospace"><em><strong>&lt;@Subject&gt;</strong></em></span></div>
                    <hr /></div>
                <br />
                Dear:<br />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;We call you to inform about :<span style="font-family:courier new,courier,monospace"><em><strong>&lt;@Your message should be here !&gt;</strong></em></span><br />
                &nbsp;
                <hr />
                <div style="float:right;">Your sincerely<br />
                    <span style="font-family:courier new,courier,monospace"><em><strong>&lt;@Your name&gt;</strong></em></span></div>
            </div>
            <input type="button" onclick="creathtmldoc()" value="creat a copy of this html page >>"/>
        </div>
    </body>
</html>

creatpage.php

    <?php
$myfile = fopen("newpage.html", "w") or die("Unable to open file!");
fwrite($myfile, $_REQUEST["data"]) or die("Unable write!");
fclose($myfile);
echo $_REQUEST["data"];
 ?>

通过这个演示,我想展示这个问题:这段代码没有做我想要的事情;它没有创建页面的副本。如何解决?

如何在 PHP 请求中使用内联 CSS 和 jQuery 发送 HTML 数据?

您是否尝试过 encodeURIComponent() javascript 函数?