1

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

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.

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 

The form looks like:

<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>

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"

My problems are:

  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')

Community
  • 1
  • 1
Snow_Mac
  • 5,727
  • 17
  • 54
  • 80
  • First of all live is depreciated try delegate ..('body').delegate('#ajaxCaptionForm','submit', function(e){ – coolguy Jun 28 '12 at 15:13
  • On is not working bcz ur version of jquery is yet to implement that..you can try delegate – coolguy Jun 28 '12 at 15:14
  • I also couldn't get delegate to work. .live works on all my other ajax loaded forms just fine. – Snow_Mac Jun 28 '12 at 15:16
  • 2
    @ubercooluk: Proper english please. Thanks. (i.e. no "bcz" and "ur") – ThiefMaster Jun 28 '12 at 15:16
  • Regarding `on`: To do what `live` does use `$(document).on('submit', '#ajaxCaptionForm', function(e) { ... })` - however, instead of `document` better use something closer to your element. If you do not have jQuery 1.7+ you can use almost the same syntax with delegate - simply swap the first two arguments. – ThiefMaster Jun 28 '12 at 15:16
  • @ThiefMaster wats with my english ?? :P and BTW on is not supported in 1.7(as he says !) so i suggested delegate – coolguy Jun 28 '12 at 15:18
  • 3
    `on` **is** supported in 1.7. he most likely just didn't use it correctly. – ThiefMaster Jun 28 '12 at 15:19
  • I'm not sure how I used it "incorrectly", I followed the manual article to a tee and nothing happened, checked syntax, selectors etc... – Snow_Mac Jun 28 '12 at 15:21
  • How important is it that the data is sent out serialized? If it's okay to send plain POST data, it might be easier to just construct a data array by iterating over the inputs. – Martijn Jun 28 '12 at 15:35
  • Lets see what you think martijn. – Snow_Mac Jun 28 '12 at 15:46

1 Answers1

1

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?

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

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

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.

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 

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.

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

ernie
  • 6,356
  • 23
  • 28