在PHP中解析jquery序列化字符串 [英] Parse a jquery serialize string in PHP

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

问题描述

在人们跳过我之前,我已经看到了这个帖子:我如何PHP - 反序列化jQuery序列化表单?

Before people jump all over me, I've seen this thread: How do I PHP-unserialize a jQuery-serialized form?

我的问题非常相似,但我的数据却大相径庭。我正在使用AJAX调用来做帖子,数据发布很好(jQuery是1.7)。形式&当用户点击几个链接并向下钻取到此形式时,动态加载AJAX& ajax脚本。

My question is very similar, yet my data is much different. I'm using a AJAX call to do the post, the data posts just fine (jQuery is 1.7). The form & AJAX are dynamically loaded in when a user clicks a few links and drills down to this form & ajax script.

AJAX看起来像:(顺便说一句,我知道你应该给我们.on()但是我似乎无法让它像我一样工作。 live())

The AJAX looks like: (BTW, I know you're supposed to us .on() but I can't seem to get that to work like I can .live() )

$('#ajaxCaptionForm').live('submit', function(e){
    e.preventDefault(); 
    $.ajax({
        'type':'POST',
        'data':{formData: $('#ajaxCaptionForm').serialize()},
        'success':function(){
            parent.$.fancybox.close();
        }
    });   
}); // closing form submit 

表格如下:

<form method="Post" action="localhost/controller" id="ajaxCaptionForm" name="ajaxCaptionForm">
    <label for="Caption">Caption</label><input type="text" id="Caption" name="Caption" value="Leaf lesions.">
    <label for="Keywords">Keywords</label>
    <p>Please seperate keywords by a comma
    <input type="text" id="Keywords" name="Keywords" value=""></p>
    <input type="hidden" id="imageID" name="imageID" value="87595">
    <input type="submit" value="Update Image" name="yt3" clicked="true">
</form>

序列化数据如下所示:(根据firebug)

The serialized data looks like: (according to firebug)

formData=Caption%3DFruit%2Blesions.%26Keywords%3D%26imageID%3D87592

当我回应一个回复时,我得到了这个:

When I echo out a response, I get this:

"Caption=Leaf+symptoms+of+++CCDV.&Keywords=&imageID=87655"

我的问题是:


  1. 关键字字段为空,即使我输入了内容

  2. 标题字段没有改变内容时改变帖子。

  3. 如何访问每个变量?标题,关键字和图像。 $ _POST也不起作用:

  1. The keywords field is empty, even when I put in content
  2. The caption field doesn't change on post when I change content.
  3. How do i access each of the variables? Caption, Keywords and Images. $_POST does not work nor:

Yii :: app() - > request-> getParam('imageID')

Yii::app()->request->getParam('imageID')


推荐答案

看来您正在制作序列化表单数据(应该已经是URL编码的键=值),作为JSON键值对中的值。这是你打算做的吗?

It appears that you're making the serialized form data (which should already be URL-encoded key=values), as the value in a JSON key-value pair. Is this what you intend to do?

来自 http://api.jquery.com/序列化/ ,请注意,通过.serialize()发送的表单数据是标准URL编码表示法中的文本字符串。

From http://api.jquery.com/serialize/, note that the form data once sent through .serialize() "is a text string in standard URL-encoded notation."

来自 http://api.jquery.com/jQuery.ajax/ ,请注意数据设置是转换为查询字符串,如果还不是字符串。

From http://api.jquery.com/jQuery.ajax/, note that the data setting "is converted to a query string, if not already a string."

因此,您将采用标准URL编码表示法中的文本字符串,然后将其作为数据设置中键值JSON对的值。

So, you're taking a text string in "standard URL-encoded notation", and then making it the value in a key-value JSON pair in data setting.

我认为你应该是这样的(忽略live()v。on()问题):

I think your could should be something like this instead (ignore the live() v. on() issue):

$('#ajaxCaptionForm').live('submit', function(e){
    e.preventDefault(); 
        $.ajax({
            'type':'POST',
            'data':$('#ajaxCaptionForm').serialize(),
            'success':function(){
                parent.$.fancybox.close();
            }
        });   
    }); // closing form submit 

这也是你不能按照期望访问任何内容的原因因为它都是在'formData'键下传递的。您可以执行print_r($ _ POST)来验证这一点,或者回显Yii :: app() - > request-> getQueryString();两者都应该打印出你作为PHP数组提交的所有数据,显示键和值。

This would also be why you can't access anything as you're expecting, as it's all being passed under the 'formData' key. You can do a print_r($_POST) to verify this, or echo Yii::app()->request->getQueryString(); both should print out all the data you've submitted as a PHP array, showing you the keys and values.

作为一个建议,这是一个完美的例子,什么时候到使用Firebug控制台确切地查看正在提交的参数。

As a suggestion, this is a perfect example of when to use the Firebug console to see exactly what params are being submitted.

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

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