SoftwareSavant
SoftwareSavant

Reputation: 9737

Serializing form in jquery and passing it into php

I have been trying for the last couple of days to serialize a form in jquery and pass it to php. For some reason it just doesn't work... Question, What steps do you take to serialize a form in Jquery... I get the id of the form pass it into $('#fomrID').serialize()...

And it still doesn't work. When I debugg with firebug All I get is an empty string... Do you use the name or the Id of the form. Do you use e.disableDefalut or whatever that is? I can't figure out why I can't serialize my form please help

    $('#profilepicbutton').change(function(){
    alert("Boy I hate PHP");
    var formElement = $('#profilepicinput').attr('value');
    dataString = $("#registerpt3").serialize();
    $.ajax({
            type: "POST",
            url: "register3.php",
            data: dataString, //"profilePic="+formElement,
            success: function(data){
                alert( "Data Saved: " + data );
                $.ajax({
                    type: "POST",
                    url: "retrievePic.php", 
                    data: "",                   
                    success: function(data){
                        alert(data);
                        var image = new Image();
                        $(image).load(function(){
                            console.log("we have uploaded this image");
                        }).attr('src', 'images/');
                        alert("");                      
                    },
                    error: function(msg){
                        alert("");
                    }
                });
            },
            error:function(msq){
                alert("" + msg);
            }
        }
    );
});


    <form  id="registerpt3" name="registerpt3" enctype="multipart/form-data" action="register3.php" onSubmit="" method="post">
<input name="profilepicinput" type="file" id="profilepicbutton"  />
</form>

Upvotes: 1

Views: 441

Answers (1)

jmif
jmif

Reputation: 1212

According to http://api.jquery.com/serialize

For a form element's value to be included in the serialized string, the element must have a name attribute. Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked. Data from file select elements is not serialized.

Also, simply calling serialize won't actually upload the picture for you. If you want to do an asynchronous picture upload (or any file for that matter) I'd suggest looking into something like Uploadify: http://www.uploadify.com/

Upvotes: 2

Related Questions