gpt4 book ai didi

php - 序列化数据未正确进入 php 数组

转载 作者:行者123 更新时间:2023-12-01 04:49:30 24 4
gpt4 key购买 nike

我正在使用此函数发布序列化数据:

$( "form" ).on( "submit", function( event ) {
event.preventDefault();
console.log($( this ).serialize());
var data = $( this ).serialize();
$.ajax({
type: "POST",
url: "content/rev/a_submit.php",
data: "data=" + data,
success: function(result){
$("#acct_content").html(result);
}
});
});

数据的console.log与表单中的内容匹配:

month1%5B%5D=4&sap=721&name=uname&month1%5B%5D=10000.00&month2%5B%5D=10000.00&month3%5B%5D=0.00&month4%5B%5D=&month5%5B%5D=&month6%5B%5D=&month1%5B%5D=15000&month2%5B%5D=0.00&month3%5B%5D=0.00&month4%5B%5D=&month5%5B%5D=&month6%5B%5D=&month1%5B%5D=50&month2%5B%5D=50&month3%5B%5D=50&month4%5B%5D=&month5%5B%5D=&month6%5B%5D=&month1%5B%5D=15000&month2%5B%5D=10000&month3%5B%5D=0&month4%5B%5D=0&month5%5B%5D=0&month6%5B%5D=0&sap_data%5B%5D=&sap_data%5B%5D=&sap_data%5B%5D=&sap_data%5B%5D= 

我 print_r 得到的 php 文件上的数组:

Array ( [0] => 10000.00 [1] => 1 [2] => 50 [3] => 15000 )** Array ( [0] => 10000.00 [1] => 1 [2] => 50 [3] => 10000 ) Array ( [0] => 0.00 [1] => 1 [2] => 50 [3] => 0 ) Array ( [0] => [1] => [2] => [3] => 0 ) Array ( [0] => [1] => [2] => [3] => 0 ) Array ( [0] => [1] => [2] => [3] => 0 ) Array ( [0] => [1] => [2] => [3] => )

问题出在第一个数组 (month1)。您可以在 console.log 中看到它显示的第二个索引:

month1%5B%5D=15000

但在数组中它有 [1] => 1

该特定索引中的每个月都会发生这种情况。

我有点失落。索引 0 和 1 在我的 php 代码中以完全相同的方式处理,尽管只有一个正确地放入数组中......

编辑:这是 html/php获取要传递的变量:

$month1= mysql_query("SELECT conf_budget,incremental,round((confidence*100),0),(updated_at) FROM rev_acct WHERE sap_id = '$sap' AND MONTH = '$mb' ORDER BY updated_at DESC LIMIT 1;");
$row1 = mysql_fetch_row($month1);
$bud1 = $row1[0];
$inc1 = $row1[1];
$con1 = $row1[2];

表单中的输入:

<td>$<input class="input-small" name="month1[]" id="conf1_<?php echo $sap; ?>" onkeypress="return isNumberKey(event)" type="text" value="<?php echo $bud1; ?>"></td>
<td>$<input class="input-small" name="month1[]" type="text" id="inc1_<?php echo $sap; ?>" onkeypress="return isNumberKey(event)" value="<?php echo $inc1; ?>" ></td>

EDIT2:添加提交代码

$month1 = array();
$month1 = $_POST['month1'];
print_r($month1);

我刚刚 var 转储了数组,并且由于某种原因索引一是 bool 。该值最初来自 mysql 表,其中存储为十进制。 (与索引 0 相同),但是当我发布它时,它以 bool 形式出现:\

最佳答案

序列化或反序列化数据没有问题,问题是你发送时的ajax调用:

data: "data=" + data

结果是 data=month1=4。这就是为什么您没有在数组中获得 4,因为您将“data”字符串连接到 data 变量。但我仍然不知道为什么你会得到 [1] => 1 以及为什么其他值的顺序不正确。

这应该有效:

$( "input[type='submit']" ).click(function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "content/rev/a_submit.php",
data: $("form").serialize(),
success: function(result){
$("#acct_content").html(result);
}
});
});

这应该可以正常工作,在解码和解析数据字符串后我得到了正确的值。请注意,不知何故,您没有正确解析 data var。尝试像我一样使用 parse_str 。另外,如果您在 a_submit.php 中的 print_r 行之前显示您所做的事情,那就太好了。

代码:

$res = "month1%5B%5D=4&sap=721&name=uname&month1%5B%5D=10000.00&month2%5B%5D=10000.00&month3%5B%5D=0.00&month4%5B%5D=&month5%5B%5D=&month6%5B%5D=&month1%5B%5D=15000&month2%5B%5D=0.00&month3%5B%5D=0.00&month4%5B%5D=&month5%5B%5D=&month6%5B%5D=&month1%5B%5D=50&month2%5B%5D=50&month3%5B%5D=50&month4%5B%5D=&month5%5B%5D=&month6%5B%5D=&month1%5B%5D=15000&month2%5B%5D=10000&month3%5B%5D=0&month4%5B%5D=0&month5%5B%5D=0&month6%5B%5D=0&sap_data%5B%5D=&sap_data%5B%5D=&sap_data%5B%5D=&sap_data%5B%5D=";
print_r(urldecode($res));
$output= array();
parse_str($res, $output);
print_r($output);

结果:

Array(
[month1] => Array
(
[0] => 4
[1] => 10000.00
[2] => 15000
[3] => 50
[4] => 15000
)

[sap] => 721
[name] => uname
[month2] => Array
...

)

关于php - 序列化数据未正确进入 php 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22942356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com