[英]How to access the serialized data in PHP file in the following scenario?
How to access the serialized data in a PHP file in following situation? 在以下情况下如何访问PHP文件中的序列化数据? The code and the serialized data is as follows:
代码和序列化数据如下:
$(document).ready(function() { $(document).on('click', '#delete_url', function (e) {
e.preventDefault();
var items = new Array();
$("input:checked:not(#ckbCheckAll)").each(function() {
items.push($(this).val());
});
var str = $("#user_filter").serialize();
$.ajax({
type: "POST",
url: "manage_users.php?op=delete_bulk_users&items="+items,
data: str,
dataType: 'json',
success: function(data) {
//var message = data.success_message;
var redirect_link = data.href;
alert(redirect_link);
window.location.href = redirect_link;
}
});
}); }); });
});
The data I'm getting in after serialize in str
is as follows: 我在
str
序列化后获得的数据如下:
op=group_filter&page=1&from_date=11%2F10%2F2000&social_login=&to_date=11%2F10%2F2013&login_criteria=all&user_name=&user_state=&user_email_id=&user_city=
Now the PHP file(manage_users.php) is as follows: 现在,PHP文件(manage_users.php)如下:
/*The code is actually one of the switch casees*/
prepare_request();
$request = empty( $_GET ) ? $_POST : $_GET ;
$op = $request['op'];
switch( $op ) {
case "delete_bulk_users":
print_r($request);/*For printing the received array of values after form submission
Here I'm not getting serialized data */
}
Thanks in advance. 提前致谢。
The serialize
function is intended to be used to create a query string for a URL from a collection of inputs (or an entire form), and will out of necessity encode characters such as /
(which denotes a directory in a URL). serialize
函数旨在用于从输入(或整个表单)的集合中为URL创建查询字符串,并且将不必要地对诸如/
(表示URL中的目录)之类的字符进行编码。 There's no way to tell .serialize()
to convert to the "proper" format because it already is . 无法告诉
.serialize()
转换为“正确”格式,因为它已经是 。
If you're using the result of .serialize()
as part of an AJAX request it's fine to do that like so: 如果您将
.serialize()
的结果用作AJAX请求的一部分,则可以这样做:
$.ajax({
url: 'yourpage.php',
data: str, // the string returned by calling .serialize()
... // other options go here
}).done(function(response) {
// do something with the response
});
Your server should handle decoding those characters when it receives the request and provide the correct values to you. 您的服务器在收到请求时应处理这些字符并为您提供正确的值。
If you're using it for something else you could try using the native JavaScript decodeURIComponent
function to convert those encoded characters back, like so: 如果您将其用于其他用途,则可以尝试使用本机JavaScript解码
decodeURIComponent
函数将这些编码的字符转换回去,如下所示:
str = decodeURIComponent(str);
Note that calling decodeURIComponent
and then trying to use it for an AJAX request won't work. 请注意,调用
decodeURIComponent
,然后尝试将其用于AJAX请求将不起作用。
For more information on URI encoding take a read through the MDN entry for encodeURIComponent
. 有关URI编码的更多信息,请仔细阅读
encodeURIComponent
的MDN条目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.