Javascript jQuery将Div内容从当前页面加载到另一页面
我见过一些从当前页面外部将HTML内容加载到页面的示例,如Javascript jQuery将Div内容从当前页面加载到另一页面,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我见过一些从当前页面外部将HTML内容加载到页面的示例,如 $("#divData").load("article.html #target"); 现在我需要做一些事情完全相反!我的意思是我需要将当前活动页面的一部分导出到服务器上现有的.php页面。例如,如果我有一个像 <!DOCTYPE HTML> <html> <body> <div class="well"> <div class="btn-group">
$("#divData").load("article.html #target");
现在我需要做一些事情完全相反!我的意思是我需要将当前活动页面的一部分导出到服务器上现有的.php页面。例如,如果我有一个像
<!DOCTYPE HTML>
<html>
<body>
<div class="well">
<div class="btn-group">
<button class="btn">Left</button>
<button class="btn">Middle</button>
<button class="btn">Right</button>
</div>
<button id="loader" class="btn btn-default">Load To PHP Page</button>
</div>
</body>
</html>
更新:
Data returned from PHP: %PDF-1.3
1 0 obj
<< /Type /Catalog
/Outlines 2 0 R
/Pages 3 0 R >>
endobj
2 0 obj
<< /Type /Outlines /Count 0 >>
endobj
3 0 obj
<< /Type /Pages
/Kids [6 0 R
]
/Count 1
/Resources <<
/ProcSet 4 0 R
/Font <<
/F1 8 0 R
>>
>>
/MediaBox [0.000 0.000 612.000 792.000]
>>
endobj
4 0 obj
[/PDF /Text ]
endobj
5 0 obj
<<
/Creator (DOMPDF)
/CreationDate (D:20140425170443+00'00')
/ModDate (D:20140425170443+00'00')
>>
endobj
6 0 obj
<< /Type /Page
/Parent 3 0 R
/Contents 7 0 R
>>
endobj
7 0 obj
<<
/Length 71 >>
stream
0.000 0.000 0.000 rg
BT 34.016 746.579 Td /F1 12.0 Tf [(salam)] TJ ET
endstream
endobj
8 0 obj
<< /Type /Font
/Subtype /Type1
/Name /F1
/BaseFont /Times-Roman
/Encoding /WinAnsiEncoding
>>
endobj
xref
0 9
0000000000 65535 f
0000000008 00000 n
0000000073 00000 n
0000000119 00000 n
0000000273 00000 n
0000000302 00000 n
0000000416 00000 n
0000000479 00000 n
0000000600 00000 n
trailer
<<
/Size 9
/Root 1 0 R
/Info 5 0 R
>>
startxref
710
%%EOF
从PHP返回的数据:%PDF-1.3
10 obj
>
endobj
20 obj
>
endobj
30 obj
>
/MediaBox[0.000 0.000 612.000 792.000]
>>
endobj
40 obj
[/PDF/Text]
endobj
50 obj
>
endobj
60 obj
>
endobj
70 obj
>
流动
0.000 0.000 0.000 rg
BT 34.016 746.579 Td/F1 12.0 Tf[(萨拉姆)]TJ等
尾流
endobj
80 obj
>
endobj
外部参照
0 9
0000000000 65535 f
000000000 800万n
0000000073000000N
0000000 11900000n
0000000273000000N
0000000 30200000n
00000004160百万元
000000047900000N
0000000 600000000N
拖车
>
起始外部参照
710
%%EOF
如果您想要活动页面的整个状态,您可以提交一个表单,该表单包含您想要的HTML输入。如果您需要的话,也可以在AJAX中执行同样的操作 例如 HTML
两个选项,首先回答您的问题,您可以使用ajax将
div.well
的内容发送到print.php
:
$.ajax({
url: 'print.php',
type: 'POST',
data: {
yourData: $('div.well').html()
},
success: function(msg) {
alert('Data returned from PHP: ' + msg);
// now do something with your returned data, liked load it in a new window.
},
error: function (xhr, ajaxOptions, thrownError) {
alert('AJAX request failed! ' + xhr.status + ' ' + thrownError);
}
});
然后,您可以echo$\u POST['yourData']在print.php
页面上的code>使用该页面上的div.well
或者,如果加载的内容是静态的(请参阅下面的注释),您可以将.load(“page.html div.well”)
放在print.php
页面上。在远程页面上使用load
仅在您不希望保留加载页面的状态(假设它是动态的)时才有效.Hi hemerlinproject谢谢,但我收到AJAX请求失败!错误here@Behseini,我上面提供的代码是示例代码,没有确切的文件名和路径,我无法与您进行故障排除。很可能是“url”的路径错误。尝试输入正确的路径,例如“http://yourdomain/any_directories/print.php”尝试上面的代码,看看我们是否可以获得关于ErrorThemerlin项目的更多数据,再次感谢,但在检查完所有内容后,我得到的都是更新的长文本,PDF中没有任何内容!
<form action="your_page.php" method="post">
<input type="hidden" name="htmlContent">
<button type="submit">Submit HTML</button>
</form>
$('form').submit(function (e) {
$(this).children('[name=htmlContent]').val($('.well').html());
});
$.ajax({
url: 'print.php',
type: 'POST',
data: {
yourData: $('div.well').html()
},
success: function(msg) {
alert('Data returned from PHP: ' + msg);
// now do something with your returned data, liked load it in a new window.
},
error: function (xhr, ajaxOptions, thrownError) {
alert('AJAX request failed! ' + xhr.status + ' ' + thrownError);
}
});