dourong6054
dourong6054
2011-09-30 22:02

在jquery中序列化表单并将其传递给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>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douwei1950 douwei1950 10年前

    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/

    点赞 评论 复制链接分享

相关推荐