Javascript 带有预定义变量的jQuery Ajax传递数组
我已经设置了一个函数来收集表单的内容并将其提交到Php页面,然后让Php页面处理它并返回一条成功消息。唯一的问题是,当我通过POST(或get)发送此内容时,实际上没有任何内容进入PHP页面。。。一点也不Javascript 带有预定义变量的jQuery Ajax传递数组,javascript,php,jquery,ajax,arrays,Javascript,Php,Jquery,Ajax,Arrays,我已经设置了一个函数来收集表单的内容并将其提交到Php页面,然后让Php页面处理它并返回一条成功消息。唯一的问题是,当我通过POST(或get)发送此内容时,实际上没有任何内容进入PHP页面。。。一点也不 function add_new_customer(){ $('.new-customer-con .submit-new-customer').click(function(){ search_path = $('.search-rel-path').val()+'i
function add_new_customer(){
$('.new-customer-con .submit-new-customer').click(function(){
search_path = $('.search-rel-path').val()+'insert/insert_new_customer';
var textinput = [];
$('.new-customer-con input:text').each(function(e){
if($(this).length > 0){
textinput[$(this).attr('name')] = $(this).val();
}
});
$('.new-customer-con textarea').each(function(e){
if($(this).length > 0){
textinput[$(this).attr('name')] = $(this).val();
}
});
var call_back_btn = $('.new-customer-con button[name=call_back]');
if(call_back_btn.text().toLowerCase() === 'no'){var call_back = 'false'; } else{var call_back = 'true'; }
textinput[call_back_btn.attr('name')] = call_back;
var shipping_method = $('.new-customer-con select');
textinput[shipping_method.attr('name')] = shipping_method.children('option:selected').val();
$.ajax({
url: search_path,
type: 'POST',
dataType: 'html', // in html mode for debugging
data: { data: textinput }
})
.done(function(data) {
console.log(data);
if(data['success']){
$('html, body').animate({scrollTop: $('.new-customer-con').offset().top }, 500);
$('.new-customer-con .alert-message').addClass('alert-success').hide().fadeIn('slow').text("Successfully inserted "+textinput['customer_name']);
var timeout = setTimeout(function(){$('.new-customer-con .alert-message').fadeOut('fast').text('').removeClass('alert-success');},2000);
}
})
.fail(function() {
console.log("error");
})
.always(function() {
});
});
}
不确定我做错了什么,但是否有更好的方法发送数组,或者我是否必须以不同的格式发送数组?抱歉,我通常知道我在用PHP做什么,但javascript中的数组有点让我头疼 尝试使用此
data:textinput
而不是data:{data:textinput}
将textinput
声明为对象
而不是数组
var textinput={}代码>
然后使用data:textinput
而不是data:{data:textinput}