Php jqueryajax表单
我从文本框中获取Php jqueryajax表单,php,jquery,ajax,Php,Jquery,Ajax,我从文本框中获取null值 日期工作正常,但我无法从输入框中获取数据 (function () { if ($('#contactform').length) { var $form = $('#contactform'), $loader = '<img src="images/preloader.gif" alt="Loading..." />'; $form.append('<div class="hid
null
值
日期工作正常,但我无法从输入框中获取数据
(function () {
if ($('#contactform').length) {
var $form = $('#contactform'),
$loader = '<img src="images/preloader.gif" alt="Loading..." />';
$form.append('<div class="hidden" id="contact_form_responce">');
var $response = $('#contact_form_responce');
$response.append('<p></p>');
$form.submit(function (e) {
$response.find('p').html($loader);
var data = {
action: "contact_form_request",
values: $("#contactform").serialize()
};
//send data to server
$.post("php/contact-send.php", data, function (response) {
$(".wrong-data").removeClass("wrong-data");
$response.find('img').remove();
if (response.is_errors) {
$response.find('p').removeClass().addClass("error type-2");
$.each(response.info, function (input_name, input_label) {
$("[name=" + input_name + "]").addClass("wrong-data");
$response.find('p').append('Please enter correctly "' + input_label + '"!' + '</br>');
});
} else {
$response.find('p').removeClass().addClass('success type-2');
if (response.info == 'success') {
$response.find('p').append('Your email has been sent!');
$form.find('input:not(input[type="submit"], button), textarea, select').val('').attr('checked', false);
$response.delay(1500).hide(400);
}
if (response.info == 'server_fail') {
$response.find('p').append('Server failed. Send later!');
}
}
// Scroll to bottom of the form to show respond message
var bottomPosition = $form.offset().top + $form.outerHeight() - $(window).height();
if ($(document).scrollTop() < bottomPosition) {
$('html, body').animate({
scrollTop: bottomPosition
});
}
if (!$('#contact_form_responce').css('display') == 'block') {
$response.show(450);
}
});
e.preventDefault();
});
}
})();
尝试将
e.preventDefault()
移动到函数的开头。否则,表单可能会在不等待执行所有jQuery的情况下提交
尝试用
$('document').ready(function() {
});
问题在于如何将POST数据分配给
$.POST
函数
麻烦代码
var data = {
action: "contact_form_request",
values: $("#contactform").serialize()
};
现在的情况是,数据以多维数组的形式返回,您需要将其包含在php页面中
尝试将$\u POST['name']
更改为$\u POST['values']['name']
,以匹配jQuery的格式。您需要对每个POST变量进行此更改。您始终可以执行var\u dump($\u POST);死亡
以确保阵列的嵌套
我应该提到,如果您想直接访问POST数据(如原始php中的数据),您应该查看文档
直接访问帖子的语法($\u POST['name']
)需要以下代码:
$.post('mypostpage.php', $('myform').serialize(), function(response) {
//do work
});
我删除了e.preventDefault(),整个页面在提交后重新加载。不,不。只要在任何
return
语句之前调用e.preventDefault()
,就将其移动到函数的开头,无论它在函数中的何处。事件处理函数的全部代码将在提交表单之前执行。谢谢先生!它工作得很好。我只是没有收到成功消息。看起来你的php代码应该用JSON响应。Response.info是您需要的。签出。完全像这样:response=$.parseJSON(response);在$.post行之后,但我删除了它,因为我遇到了错误。你应该打开一个新的问题来解决这个问题,这样我们就可以继续讨论这个主题了。发布输出的PHP代码和jQuery。我们需要看看你如何发送回复。
$.post('mypostpage.php', $('myform').serialize(), function(response) {
//do work
});