Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在php中序列化JS返回字符串中的输入_Javascript_Php_Ajax_Json - Fatal编程技术网

Javascript 在php中序列化JS返回字符串中的输入

Javascript 在php中序列化JS返回字符串中的输入,javascript,php,ajax,json,Javascript,Php,Ajax,Json,我的JS代码有点问题 我有一个AJAX,我想用PHP获取变量 除“param”数据外,所有操作都正常 代码如下: $('#signup_submit').click(function (event){ param = jQuery('input').filter(function() { return this.name.match(/param/); }).serialize(); event.preve

我的JS代码有点问题

我有一个AJAX,我想用PHP获取变量

除“param”数据外,所有操作都正常

代码如下:

$('#signup_submit').click(function (event){
    param = jQuery('input').filter(function() {
                     return this.name.match(/param/);
                }).serialize();
    event.preventDefault();
    $.ajax({
        type: "POST",
        url: result.php,
        data: {
            action: 'submit_ajax',
            email: $('#email').val(),
            send_welcome: true,
            listId:  $('#listid').val(),
            param: param
        },
        success: function(msg) {
            console.log('ok');
        }
    });
});
参数(PHP)的结果var_dump:

应该是这样的:

array(1){ 'confirmation'=>1 }
有什么好办法吗

谢谢

使用而不是序列化

param = jQuery('input').filter(function() {
    return this.name.match(/param/);
}).serializeArray();
使用而不是序列化

param = jQuery('input').filter(function() {
    return this.name.match(/param/);
}).serializeArray();
%5B是[ %5D是]

您可以在提交之前使用此函数修复编码

function fixedEncodeURI (str) {
    return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']');
}
像这样:

$('#signup_submit').click(function (event){
    param = jQuery('input').filter(function() {
                     return this.name.match(/param/);
                }).serialize();
    event.preventDefault();
    $.ajax({
        type: "POST",
        url: result.php,
        data: {
            action: 'submit_ajax',
            email: $('#email').val(),
            send_welcome: true,
            listId:  $('#listid').val(),
            param: fixedEncodeURI(param)
        },
        success: function(msg) {
            console.log('ok');
        }
    });
});
%5B是[ %5D是]

您可以在提交之前使用此函数修复编码

function fixedEncodeURI (str) {
    return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']');
}
像这样:

$('#signup_submit').click(function (event){
    param = jQuery('input').filter(function() {
                     return this.name.match(/param/);
                }).serialize();
    event.preventDefault();
    $.ajax({
        type: "POST",
        url: result.php,
        data: {
            action: 'submit_ajax',
            email: $('#email').val(),
            send_welcome: true,
            listId:  $('#listid').val(),
            param: fixedEncodeURI(param)
        },
        success: function(msg) {
            console.log('ok');
        }
    });
});

您发布的
$param
var看起来像
param[确认]=1

您需要
parse_str
函数来获取序列化数据:

parse_str($param, $output);
输出

var_dump($output);

array(1) {
  ["param"]=>
  array(1) {
    ["confirmation"]=>
    string(1) "1"
  }
}
或直接获取具体索引:

parse_str($param);
echo $confirmation; // 1

您发布的
$param
var看起来像
param[确认]=1

您需要
parse_str
函数来获取序列化数据:

parse_str($param, $output);
输出

var_dump($output);

array(1) {
  ["param"]=>
  array(1) {
    ["confirmation"]=>
    string(1) "1"
  }
}
或直接获取具体索引:

parse_str($param);
echo $confirmation; // 1

输出是因为
param
正在通过
.serialize()
进行双重编码:

对于
数据:
,由
$.ajax()运行
$.param()

当为
$\u POST
解码第一轮编码时:

var_dump($_POST['param']);
// string(25) "param%5Bconfirmation%5D=1"
您必须使用以下方法手动解码第二轮:


您还可以尝试通过将
param
与其他编码数据连接起来来避免双重编码:

data: param + '&' + $.param({
    action: 'submit_ajax',
    email: $('#email').val(),
    send_welcome: true,
    listId:  $('#listid').val()
}),

输出是因为
param
正在通过
.serialize()
进行双重编码:

对于
数据:
,由
$.ajax()运行
$.param()

当为
$\u POST
解码第一轮编码时:

var_dump($_POST['param']);
// string(25) "param%5Bconfirmation%5D=1"
您必须使用以下方法手动解码第二轮:


您还可以尝试通过将
param
与其他编码数据连接起来来避免双重编码:

data: param + '&' + $.param({
    action: 'submit_ajax',
    email: $('#email').val(),
    send_welcome: true,
    listId:  $('#listid').val()
}),

在for循环中创建一个关联数组,然后
json_encode
该数组并回显到js。尝试调用console.log(param)并告诉我们结果。在for循环中创建一个关联数组,然后
json_encode
该数组并回显到js。尝试调用console.log(param)请告诉我们结果。谢谢你的解释。真的很有帮助。谢谢你的解释。这真的很有帮助。