用jquery解码.serialize()

时间:2011-04-12 09:16:24

标签: jquery ajax

我都是。我昨天发布了一个关于未使用jquery-ajax发送的日期的问题,并找到了一个带有.serialize()函数的解决方案。

不幸的是,serialize()函数解码了日期输入,我得到的结果是:

arrival= 13%2F04%2F2011 &departure= 28%2F04%2F2011 &ap_ID=2

我知道如何将decodeURIComponent()用于单个字段,如下所示:

 var arrivalVal = $('#arrival').val(); 

 arrivalVal = decodeURIComponent(arrival);

但是如何将它用于.serialize()函数生成的长字符串?

很抱歉,如果这似乎是一个非常明显的问题,但无法绕过它。

4 个答案:

答案 0 :(得分:5)

尝试使用

var editserialize = $('form#edit').serialize();
editserialize = decodeURIComponent(editserialize.replace(/%2F/g, " "))

...

答案 1 :(得分:2)

对于php,您可以执行urldecode功能http://php.net/manual/en/function.urldecode.php

答案 2 :(得分:1)

function decodeSerialize($QUERY_STRING, $valueSpecChars = false)
{
    $array = array();
    $a = explode('&',$QUERY_STRING); 

    $i = 0;
    while ($i < count($a)) {
      $b = explode('=', $a[$i]);
      $ar = preg_replace('/\[/', '][', htmlspecialchars(urldecode($b[0])), 1);
      $ar = str_replace(array('[',']'), array('["','"]'), $ar);
      $val = urldecode($b[1]);
      if($valueSpecChars) $val = htmlspecialchars($val);
      else $val = str_replace('"', '\"', $val);
      eval('$array["'.$ar.'="'.$val.'";');
      $i++;
    }

    return $array;
}

此函数返回一个与$ _POST完全相同的数组。

答案 3 :(得分:0)

    Try This:
            <?php 
            /*
             * As a Class File your-class-file.php
             * Decode jQuery Serialize Post String For PHP
             */
             class PHP_Serialize {
             function Decode_Serialize($QUERY_STRING) {
                      /*
                       * Decode form.serelize() jQuery Post String
                       * Return like $_POST['Form_Input_Name or ID']
                       */
                      $a = explode('&', $QUERY_STRING);
                      $i = 0;
                      $store = array();
                      while ($i < count($a)) {
                          $b = explode('=', $a[$i]);
                          $array_name = htmlspecialchars(urldecode($b[0]));
                          $array_value = htmlspecialchars(urldecode($b[1]));
                          $store[$array_name] = $array_value;
                          $i++;
                      }
                       /*
                        * Convert Array as an Object
                        * return(object)$store; 
                        * Use ........Object->Form_Input_Name or ID
                        * or
                        * Use as An Array .........$var["Form_Input_Name or ID"]
                        */
                      return $store;
                  }
             }
            ?>
            <?php 
            /*
             * How to USE:
             * In Your PHP File (your-file.php)
             * Include Class File like include('your-class-file.php');
             * $Decode = new PHP_Serialize();
             * Use Function Like This $_POST = $Decode->Decode_Serialize($_POST['Query_String']);
             */

            /*
             * Demo HERE
             * you-file.php
             */
            include('your-class-file.php');
            $Decode = new PHP_Serialize();
            $_POST = $Decode->Decode_Serialize($_POST['Query_String']);
            /* Decode jQuery Serlize String AS a PHP Function if you not making Class file */
            $_POST = Decode_Serialize($_POST['Query_String']);
            /* Use POST String in your mySQL Statement Like This*/
            mysql_query("SELECT * FROM Table WHERE COMPANY_NAME = '".$_POST['COMPANY_NAME']."'");


            /*
             * Here is Your form PHP File
             */

            ?>
            <form action="" method="post" id="your-form-id" name="your-form-id">
            <label for="COMPANY_NAME">Company Name:</label>
            <input type="text" name="COMPANY_NAME" id="COMPANY_NAME"/>
            <label for="COMPANY_EMAIL">Company Email:</label>
            <input type="text" name="COMPANY_EMAIL" id="COMPANY_EMAIL"/>
            <input type="button" name="submit" id="submit" value="submit" />
            </form>


            <script>
            $(document).ready(function() {
                $('#submit').click(function(){

                    var Post_String = $('#your-form-id').serialize();
                    $.ajax({
                    type    :   'POST',
                    url     :   'your-file.php',
                    data    :   {Query_String:Post_String},
                    success :   function(return_value){/* On Success you jQuery Code Here*/}
                    });

                });

            });
            </script>