[解决]
这是有史以来最难的错误 - 所有这都归咎于复制/粘贴内容。
此:
$('#errors'+bUID).append('<ul id="error_list"'+bUID+'></ul>');
应该是:
$('#errors'+bUID).append('<ul id="error_list'+bUID+'"></ul>');
该死的&#39; + bUID +&#39;被粘贴在&#34; ,不是之前!
当然它无法附加任何东西...... 2周...... 2周浪费! )))
这里是js:
$('form').submit(function(e){
bUID = $(this).find('input[name=bUID]').data("buid");
e.preventDefault();
submitForm(bUID);
alert(bUID);
});
function submitForm(bUID) {
var name = $('#name'+bUID).val();
var email = $('#email'+bUID).val();
var message = $('#message'+bUID).val();
var code = $('#code'+bUID).val();
alert(bUID);
// also tried this
var post_data = {
'name': $('#name'+bUID).val(),
'email': $('#email'+bUID).val(),
'message': $('#message'+bUID).val(),
'code': $('#code'+bUID).val(),
'buid': bUID,
};
alert(Object.keys(post_data).length);
// ALSO tried this instead of ajax:
//$.post($('#contact_form'+bUID).attr('action'), post_data, function(response){
alert(response);
$.ajax({
dataType: "json",
type: "post",
data: "name=" + name + "&email=" + email + "&message=" + message + "&code=" + code + "&buid=" + bUID,
//data: post_data,
url: $('#contact_form'+bUID).attr('action'),
success: function(response) {
if (typeof response !== 'undefined' && response.length > 0) {
if (response[0] == "success") {
$('#success'+bUID).append('<p>Success</p>');
}
else {
$('#errors'+bUID).append('<p>' + js_errors + '</p>');
$('#errors'+bUID).append('<ul id="error_list"'+bUID+'></ul>');
$.each(response, function(i, v){
if (i > 0) {
$('#error_list'+bUID).append('<li>' + v + '</li>');
}
});
}
}
}
});
}
这里是view.php中的操作:
<?php
$bUID = $controller->getBlockUID($b);
$form = Loader::helper('form');
$formAction = $view->action('submit', Core::make('token')->generate('contact_form'.$bUID));
?>
<form id="contact_form<?php echo $bUID; ?>"
class="contact-form"
enctype="multipart/form-data"
action="<?php echo $formAction?>"
method="post"
accept-charset="utf-8">
<?php echo $bUID; ?><br />
<input type="hidden" name="bUID" data-buid="<?php echo $bUID; ?>" data-popup="<?php echo $popup; ?>">
...etc.
这里是controller.php:
public function action_submit($token = false, $bID = false)
{
$this->form_errors = array();
array_push($this->form_errors, "error");
array_push($this->form_errors, $_POST['name']);
array_push($this->form_errors, $_POST['email']);
array_push($this->form_errors, $_POST['message']);
array_push($this->form_errors, $_POST['code']);
array_push($this->form_errors, $_POST['buid']);
echo Core::make('helper/json')->encode($this->form_errors, JSON_UNESCAPED_UNICODE);
exit;
}
它获取所有数据并在警报中显示它,但随后在控制台中显示以下错误:
Uncaught TypeError: Cannot use 'in' operator to search for 'length' in ["error","gggg","gggg@gmail.commm","gggggggggggggggggggggggg","gggg","171"]
at r (jquery.js:2)
at Function.each (jquery.js:2)
at Object.success (view.js:132)
at j (jquery.js:2)
at Object.fireWith [as resolveWith] (jquery.js:2)
at x (jquery.js:5)
at XMLHttpRequest.b (jquery.js:5)
js文件的第132行是:$ .each(response,function(i,v){
我无法弄清楚出了什么问题。警报工作并返回输入的数据:&#34;错误,gggg,gggg @ gmail.commm,gggggggggggggggggggggg,gggg,171&#34;,但php重新打造空对象:&#34; [&#34;错误&#34; ,NULL,NULL,NULL,NULL,NULL]&#34; - $ _POST是空的!
这里有什么问题?为什么表格没有发布?
非常感谢。
答案 0 :(得分:0)
您是否尝试过添加return false;
以阻止您的表单提交所需的操作?
$('form').submit(function(e){
bUID = $(this).find('input[name=bUID]').data("buid");
//e.preventDefault();
//e.stopPropagation();
submitForm(bUID);
alert(bUID);
return false;
});
答案 1 :(得分:0)
试试这种方式,
function submitForm(bUID) {
var name = $('#name'+bUID).val();
var email = $('#email'+bUID).val();
var message = $('#message'+bUID).val();
var code = $('#code'+bUID).val();
$.post($('#contact_form'+bUID).attr('action'), {name:name, email:email, message:message, code:code, buid:bUID}, function(result){
alert(result);
});
}
答案 2 :(得分:0)
您的post_data
变量是正确的。现在,您的ajax中的data
属性是错误的 - 它是GET
格式(字符串),而不是POST
。正确的方法(json)是;
$.ajax({
dataType: "json",
type: "post",
data: {
name: nameVar,
email: emailVar,
message: messageVar
},
url: ...,
success: function(data) {
...
}
});
我&#34;重命名&#34;您的变量可以尝试避免使用与键名称相同的变量(例如,您希望发布&#34;名称&#34;设置变量&#34;名称&#34;可能会发生冲突)。
答案 3 :(得分:0)
只需使用
data: form.serializeArray()
像这样:
$.ajax({
url: 'url to post data',
dataType: "json",
method: "post",
data: form.serializeArray(),
success: function(data) {
// another staff here, you can write console.log(data) to see what server responded
},
fail: function(data) {
console.log(data) // if any error happens it will show in browsers console
}
});
另一个提示:在服务器端,您可以使用http_response_code(200)表示成功,http_response_code(400)表示错误,http_response_code(403)如果需要授权