我通过AJAX Post从jquery发送数据,所有表单字段都序列化(在WordPress中)。
$('#SendEmail').click(function () {
jQuery.post(
the_ajax_script.ajaxurl,
{
'action': 'the_ajax_hook_SendEmailWithCalculatorContent',
'calculatorFormContent': $("#CalculatorForm input").serialize()
},
function (response_from_the_action_function) {
$("#G1").val(response_from_the_action_function);
}
);
});
以下是来自Firebug POST的帖子参数:
action the_ajax_hook_SendEmailWithCalculatorContent
calculatorFormContent ZIPCodeTextBox=01915&G1=PhoneNumber+%3A++ZipCode+%3A+&G2=&G3=&G4=&G5=&G6=&G7=&G8=&G9=&G10=&G11=&G12=&G13=&G14=&G15=&G16=&G17=&G18=&G19=&G20=&G21=&G22=&G23=&G24=&G25=&G26=&G27=&fullname=&email=&phone=7324211531
当我尝试访问这些参数时,需要在 PHP 中访问它们
echo "PhoneNumber : ".$_POST['phone']." ZipCode : ".$_POST['ZIPCodeTextBox']." IndexValue : ".$_POST['0'];
我得到的输出是"电话号码:邮政编码:索引值:",所以所有表单数据都是空白的。
当我尝试时:
echo $_POST;
我看到值"数组"遇到。如何访问数组值?
在这里看到多个帖子使用参数名称回答,可以从 $_POST['name'] 获取数据。(类似链接:jQuery AJAX 表单数据使用 PHP 序列化)但显然它在我的情况下不起作用。
那么'calculatorFormContent': JSON.stringify($("#CalculatorForm input").serializeArray())
呢,然后在你的PHP脚本中使用$array = json_decode($_POST['calculatorFormContent'], true);
稍后使用 $array['phone']
访问它。