我想通过jQuery发布div的内容,重定向到php文件,然后回显php文件中的div内容。
HTML
<div id="edit_content">
<p>This is a test</p>
</div>
<a href="processingscript.php" id="submit_script">Submit</a>
jquery
$('#submit_script').click(function(){
$.post('/processingscript.php', {'html': $('#edit_content').html()};
});
PHP
$content = $_POST['html'];
echo $content;
到目前为止,我在php文件上得到了一个空白回显。我应该使用$()。redirect()?
答案 0 :(得分:1)
嗯..我不完全确定你在这里要做什么。但这就是我的理解 -
您想要在不同的PHP脚本上访问div的内容。您使用单词“ redirect ”,然后使用jQuery AJAX方法。最后,你还没有为AJAX调用定义一个回调,所以你永远不会看到来自PHP的回声。
尝试这样的事情 -
var htmlData = $('#edit_content').html();
$.post('/processingscript.php', {'html': htmlData },function(response){
// response now contains anything echoed from processingscript.php
});
您在$.post()
函数中看到的匿名函数是我之前提到的回调。这是一个AJAX请求成功完成后立即调用的函数。传递给它的参数(response
)是processingscript.php
脚本的输出。
因此,要将整个事物组合在一起,您的jQuery会向您的PHP脚本发送一个AJAX请求,然后可以根据需要操作和使用$_POST
数据。一旦PHP完成它的业务,它就可以回显状态消息。这可以是纯文本,只是“ Woo Hoo!脚本完成!”,甚至是包含您想要传递回JavaScript的任何信息的对象(这将是JSON响应)。
答案 1 :(得分:1)
我想这就是你想要的。经过测试,100%对我有效
的 HTML:强> 的
<div id="edit_content">
<p>This is a test paragraph</p><p> This is <u>another</u> <b>paragraph</b></p>
</div>
<button id="submit_script">Submit</button>
<div style="border: 2px solid red; width: 200px; height: 100px;">
<b>Result:</b>
<div id="result"></div>
</div>
的使用Javascript:强> 的
$(document).ready(function() {
$("#submit_script").click(function() {
var content = $('#edit_content').html();
$.post("processing.php", { html: content})
.done(function(data) {
$('#result').empty().append(data)
});
});
});
这会在新div上加载返回的div内容。
答案 2 :(得分:0)
你应该添加
return false;
$('#submit_script').click(function(){
$.post('/processingscript.php', {'html': $('#edit_content').html()};
return false;
});
并且请记住,您只能在浏览器的javascript控制台中看到您通过AJAX请求获得的页面中显示的内容。您插入的“echo”在您的页面上没有显示任何内容,除非您使用jQuery获得响应,但脚本没有意义。