简体   繁体   English

在以下情况下如何访问PHP文件中的序列化数据?

[英]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.

相关问题 在以下情况下如何访问json数据并在if条件下使用? - How to access json data and use in if condition in following scenario? 如何访问序列化表格数据并进行处理? - How to access serialized form data and process it? 在以下情况下如何在$ .ajax()函数的data属性中发送参数? - How to send a parameter in data attribute of $.ajax() function in following scenario? 在以下情况下如何将数据追加到jQuery中的特定div? - How to append data to a particular div in jQuery in the following scenario? 如何在以下情况下实现自动完成 - How to implement autocomplete for the following scenario jQuery-在其他位置访问序列化数据 - jQuery - Access serialized data elsewhere 如何在JavaScript中访问序列化对象中包含的数据 - How to access the data contained within a serialized object in javascript 在 PHP 中访问序列化的表单数据 - Accessing serialized form data in PHP 如何在以下情况下将数组从smarty模板分配给javascript,并在javascript中访问相同的分配数组? - How to assign array from smarty template to javascript and access the same assigned array in javascript in following scenario? 在以下情况下,如何使代码可用于多个文件文件对象? - How to make the code workable for more than one file file object in following scenario?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM