jQuery to PHP-序列化的字符串 [英] jQuery to PHP - serialized strings

查看:99
本文介绍了jQuery to PHP-序列化的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用AJAX登录表单.

I'm currently AJAX'ing a login form.

var data = $(this).serialize();

这是我正在发送的数据,我可以通过PHP使用Codeigniter来获得,就像这样:

This is the data I'm sending, I get this with PHP using Codeigniter like so:

$ajax = $this->input->post('data');

这将返回您期望的username=john&password=doe.

如何将其转换为数组?我尝试做unserialize()却得到空值.

How do I turn that into an array? I've tried doing unserialize() and I get null.

推荐答案

我相信您可以使用PHP的parse_str()函数:

I believe you can use PHP's parse_str() function: http://php.net/manual/en/function.parse-str.php

<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first;  // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz

parse_str($str, $output);
echo $output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz

?>

使用您的代码将是:

parse_str($this->input->post('data'), $ajax);
echo $ajax['username'] . "/" . $ajax['password'];

这篇关于jQuery to PHP-序列化的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆