我在我的MySql数据库中使用javascript添加了一条新记录,我必须在PHP中详细说明这个功能。
$(document).on('submit', '#product_form', function(event){
event.preventDefault();
//btn_action="add_pricelvl"; //Set variable to call the add new item
var valdata = $(this).serialize(); //Array with field value
var tax = $('#item_tax').val(); //checkbox tax
var taxvalue = $('#item_taxvalue').val(); //inputbox tax
var tabledets = it_det //Read the detail table
.rows()
.data();
var arr1=[];//Declare the array
var i=0;
//Put the datatable(item_details) rows in the array
for (i=0; i<tabledets.length; i++){
arr1[i]=tabledets.rows(i).data();
}
//call ajax function and send variable to php file.
$.ajax({
processData: false,
url:'item_action.php',
method:"POST",
data:{
//btn_action:btn_action,
valdata:valdata,
tax:tax,
taxvalue:taxvalue,
arr1:arr1
},
success : function(data)
{
....
}
error : function () {
....
}
})
});
&#13;
<?php
if ($_POST['btn_action']=='add_pricelvl'){
$query="....."
//Getting variables by JavaScript
}
?>
&#13;
我无法在我的PHP文件中获取任何变量......这怎么可能?
试图检查PHP文件中的任何$_POST[variable]
它们是NULL ...为什么?
答案 0 :(得分:0)
尝试删除processData: false,
。该命令告诉jQuery不要将对象转换为查询字符串。如果您想在脚本中使用$_POST[]
,则可以将其作为查询字符串发送。
http://api.jquery.com/jQuery.ajax/
processData(默认值:true)
类型:布尔
默认情况下,作为对象传入数据选项的数据(技术上,不是字符串)将被处理并转换为查询字符串,适合默认内容类型“application / x-www-form-urlencoded” 。如果要发送DOMDocument或其他未处理的数据,请将此选项设置为false。
答案 1 :(得分:-1)
要从POST电话中获取正文,您应该使用类似这样的内容
$json = file_get_contents('php://input');
$json = urldecode($json);
$obj = json_decode($json, true);
然后你可以将$ obj变量作为常规php数组来操作。