Php 使用ajax在colorbox中提交表单
到目前为止,我一直在使用此代码在色盒窗口中发布数据,直到遇到编码错误,数据以“ΞΞ§ΞΞΞΞΞ”而不是“ΔΝΡΡΜΞ”的形式提交:Php 使用ajax在colorbox中提交表单,php,ajax,colorbox,Php,Ajax,Colorbox,到目前为止,我一直在使用此代码在色盒窗口中发布数据,直到遇到编码错误,数据以“ΞΞ§ΞΞΞΞΞ”而不是“ΔΝΡΡΜΞ”的形式提交: $("#productadd").submit(function(){ $.post( $(this).attr('action'), $(this).serialize(), function(data){ alert('Product added to cart!'); $().colorbox.close(); } );
$("#productadd").submit(function(){
$.post(
$(this).attr('action'),
$(this).serialize(),
function(data){
alert('Product added to cart!');
$().colorbox.close();
}
);
经过一些搜索后,我决定将代码更改为此,以便在发布时设置编码,但我需要一些帮助来完成此操作:
$.ajax({
type: "POST",
url: $(this).attr('action'),
contentType: "application/json; charset=iso-8859-7",
dataType: "json",
data: "{id: '" + someId + "'}",
success: function(json) {
alert('Product added to cart!'),
$().colorbox.close(),
$("#success").html("json.length=" + json.length);
itemAddCallback(json);
},
error: function (xhr, textStatus, errorThrown) {
$("#error").html(xhr.responseText);
}
});
但是success:内部没有调用,页面被重定向到表单操作中的url,也没有调用警报或$().colorbox.close(),而在前面的代码中,它用于在同一窗口中提交(没有重定向到操作url)并显示警报,最后关闭colorbox窗口。
有什么建议吗?您必须通过阻止正常表单提交来执行默认操作, 否则,它将作为正常表单提交执行重定向到表单的url
$("#productadd").submit(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: $(this).attr('action'),
contentType: "application/json; charset=iso-8859-7",
dataType: "json",
data: "{id: '" + someId + "'}",
success: function(json) {
alert('Product added to cart!'),
$().colorbox.close(),
$("#success").html("json.length=" + json.length);
itemAddCallback(json);
},
error: function (xhr, textStatus, errorThrown) {
$("#error").html(xhr.responseText);
}
});
});
我相信这可以解决您提交表单的问题
$("#productadd").submit(function(event){
event.preventDefault(); // prevent submission of the form and send ajax
$.post(
$(this).attr('action'),
$(this).serialize(),
function(data){
alert('Product added to cart!');
$().colorbox.close();
}
);
至于编码,它可能取决于您获得的浏览器编码,请尝试更改为其他希腊语编码或UTF-8。我当前的编码是iso-8859-7,所有表单提交都很好(希腊语),除了我通过jquery/ajax使用的表单。试试UTF-8它应该可以工作!另外,请看一看这个,它是一种不同的编码,但它显示了一些可能的编码问题解决方案不幸的是,数据库中的数据使用Windows编码,因此我需要坚持使用iso-8859-7并解决这个问题。您可以更改数据库的编码,并使用utf-8,它支持希腊语以及其他许多语言,祝您好运