我试图通过jquery保存正在对服务器工作的文档的当前dom。但是遇到了一个问题,试图将dom值发送到处理保存的php文件。下面是代码:
Jquery$('SaveQuote').click(function() {
var customername = $('#customername').val();
var customernameedit = $.trim(customername);
var customerphone = $('#customerphone').val();
var quotedata = encodeURIComponent($(document).html());
$( "#Message" ).load("/include/SaveQuote.php?
customername="+customernameedit+"&customerphone="
+customerphone+"&data="+quotedata);
});
PHP代码$customername = $_GET['customername'];
$thedata = $_GET['data'];
$thephone = $_GET['customerphone'];
$file = $_SERVER['DOCUMENT_ROOT'].'/Quotes/'.$customername.$thephone.'.html';
// Open the file to get existing content
$current = $thedata;
// Append a new person to the file
file_put_contents($file, $current);
echo'Record Was Saved! View It Here <a href="/Quotes/"'.$customername.$thephone.'>
View Quote</a>';
?>
加载请求正在工作,所有的值都是"张贴",但加载请求不允许dom通过jquery代码中的quotedata变量发送,可能是在字符串中有太多的数据?
我相信我可能必须使用Jquery的post,但不是100%确定。
这段代码的目的是允许在操作期间保存dom,以便稍后查看或更改/编辑,因此我需要按原样保存整个dom。最终也会实现自动保存,但不是这个问题的一部分。
简单地说,如何通过jquery的ajax控件将当前dom发送到php保存文件你的javascript可以是这样的:
$('SaveQuote').click(function() {
var customername = $('#customername').val();
var customernameedit = $.trim(customername);
var customerphone = $('#customerphone').val();
var quotedata = $('html').html();
$.ajax({
url: "/include/SaveQuote.php",
type: "POST",
data: {
customername: customernameedit,
customerphone: customerphone,
data: quotedata
}, success: function(response) {
$("#Message").html(response);
}
});
});
这将向服务器发出带有所需数据的POST请求,并且服务器响应将作为innerHTML注入#Message
元素。