Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I access form data from a JQuery form object?

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
I have an on.submit handler, which I assume is being passed the form to $(this), how do I access the form data?

Code:
    $('#financial_promotion').on('submit',function(e) {
        e.preventDefault();
        showDialog('We are processing your financial promotion.','Please Wait!',[]); 
        sendFP($(this));   
    });

OK, but now what?

Code:
// Send FP form
function sendFP(frm)
{
    frm.????
}

I'm obviously missing something real simple, but I can't work it out.

Thanks,
1DMF


"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
Code:
$('#financial_promotion').on('submit',function(e) {
        e.preventDefault();
        var frmObj = $(this).get(0);
        var formData = new FormData(frmObj);
        $.ajax( {....} );  
    });
 
What do you want to do with the form data?

If you submit the form to the server, you would need a server side script to process that data. Usually inside the POST variable.

If you want to access it from jquery you can target the form elements individually to get their contents.

Something like:

Code:
alert(frm.elements.elementName.value);

that would theoretically alert the value of an element such as an input.







----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Justin is close to what I'm trying to achieve (I think), this is what I have...

Code:
// Send FP form
function sendForm(frm)
{

    // create formdata object from form
    var data = new FormData(frm);
    
    // check if filereader shim used and manually add file if there is one
    if(!$('#fp_file').val() && myFile)
    {
        data.append('fp_file', myFile, myFile.name);
    }

    $.ajax({
        url: frm.attr('action'),  // server script to process data
        type: 'POST',
        xhr: function() {  // custom xhr
            var myXhr = $.ajaxSettings.xhr();
            if(myXhr.upload){ // check if upload property exists
                myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // for handling the progress of the upload
            }
            return myXhr;
        },
        data: data, 
        cache: false,
        contentType: false,
        processData: false
    });

}

This is a far as I have got, I've yet to implement the progress meter with the <progress> element let alone see if I can actually read the form data and file server side.

I'll look at this again Monday, not finding this fun at all, so gonna do some painting this weekend instead of coding.

Have a great weekend guys and gals.

1DMF

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
if you're going to upload files you need to set the contentType to multipart/form-data too.

i think if you want to add a binary file something like this would be more likely

Code:
data.append("myFile", $('#fp_file').get(0).files[0]);
 
if you're going to upload files you need to set the contentType to multipart/form-data too.
Not according to all the information I can find on this. Everything I have found says you MUST set it to 'false' for this to work.

I should have a handle on this today, so will let you know what I find.

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top