简体   繁体   English

如何在php中从javascript解码序列化url

[英]how to decode serialize url from javascript in php

please consider following snippet i have submited a form which contains a background image url , i have serialize the form data .请考虑以下代码段,我提交了一个包含背景图像 url 的表单,我已经序列化了表单数据。 in php the URL is not decoding , how to get orignal url在 php 中,URL 未解码,如何获取原始 url

$("#slider_settings_form").submit(function(e) {
    var postData = $(this).serialize();
    submited form
    $.ajax({
        url: ajaxurl,
        data: {
            "params": postData,
            "action": "saveFormSettings"
        },
        method: "POST",
        success: function(response) {
            alert(response);
        },

    });

使用string urldecode( string $str )函数解码您编码的 URL 数据以获取更多信息,请点击此链接

A successful parsing could be done by adding urldecode like this:可以通过像这样添加urldecode来成功解析:

parse_str(urldecode($_REQUEST['params']), $params);

urldecode is important because it converts url encoded string into parsable string. urldecode很重要,因为它将 url 编码的字符串转换为可解析的字符串。

If you want to achieve it from javascript you can use these methods:如果你想从 javascript 实现它,你可以使用这些方法:

var uri = "http://stackoverflow.com/questions/30587877/how-to-decode-serialize-url-from-javascipt-in-php"
var uri_enc = encodeURIComponent(uri); //for encoding
var uri_dec = decodeURIComponent(uri_enc); //for decoding

Here is the link for more details: Url decode and encode这是更多详细信息的链接: Url decode and encode

Have a look at this related question .看看这个相关问题

We need to see how you're decoding the data in PHP to help you, but in addition to the answer ahead of mine (suggesting the use of urldecode ), you should also make sure postData actually has data in it.我们需要了解您如何在 PHP 中解码数据以帮助您,但除了我前面的答案(建议使用urldecode )之外,您还应该确保postData确实有数据。

Only "successful controls" are serialized to the string [...] - second answer只有“成功控制”被序列化为字符串 [...] -第二个答案

It's entirely possible postData is nil. postData完全有可能为零。 You should test it by alerting it and go from there.你应该通过提醒它来测试它并从那里开始。 The question I linked to has a more thorough answer with code examples.我链接到的问题对代码示例有更全面的回答。

var postData = $(this).serialize(); -- this would create a query string like 'a=1&b=2' , where a and b are form fields. -- 这将创建一个类似于'a=1&b=2'的查询字符串,其中ab是表单字段。 You might want to fetch the value of a or b -- the following code will help you:您可能想要获取ab的值——以下代码将帮助您:

parse_str($_GET['params'], $params);
// then you can use $params['a'] to fetch form field 'a'
print_r($params);
// => 
//Array
//(
//    [a] => 1
//    [b] => 2
//)

For more about parse_str , see http://php.net/manual/en/function.parse-str.php有关parse_str更多信息,请参阅http://php.net/manual/en/function.parse-str.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM