SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Send forms with POST/GET methode via Ajax

    Hi All,

    I have a form with POST methode. I'm looking a JavaScript that can find ALL form elements and than sends it with AJAX.

    On this page http://www.captain.at/howto-ajax-form-post-get.php you can see one example.

    But, my form's elements are located in tables, divs and span objects. For this, this sample dos not work for me.

    Can anyone help me?

  2. #2
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use this function:
    Code:
    function getPostStringFromForm( form )
    {
    	var postData = '';
    	if( typeof form == 'string' ) var form = document.getElementById( form );
    
    	var result = new Array();
    	var formElements = new Array( 'input', 'textarea', 'select' );
    
    	for( var i = 0; i < formElements.length; i++ )
    	{
    		els = form.getElementsByTagName( formElements[i] );
    
    		for( var j = 0; j < els.length; j++ )
    		{
    			result.push( els[j] );
    		}
    	}
    
    	for( var i = 0; i < result.length; i++ )
    	{
    		var name = result[i].name || result[i].id;
    
    		postData += name + '=' + result[i].value + '&';
    	}
    
    	return postData.replace(/&$/,'');
    }

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What I have to change for GET methode?

    Thank you.

    What I have to change if my form is GET methode?

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't have to change anything, just append the string you get from the function to the URL you call with Ajax.

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This code gets ALL form elements names and their values.

    It gets all checkbox and radios WITHOUT looking it's selected or checked.

    How can I correct this?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •