Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Ajax post help!

  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    Ajax post help!

    Hello,
    I'm having trouble with posting data from a dynamic collection.

    my code:
    PHP Code:

    function TestPost(strForm){

        
    xmlHttp=GetXmlHttpObject()
        var 
    str FormCollection(strForm); 
        
    //var str = "firstname=ken&lastname=scott";
        
    var url "post.asp";
        
    xmlHttp.open("POST",url,true); 
        
    xmlHttp.setRequestHeader("Content-Type""application/x-www-form-urlencoded; charset=UTF-8"); 
        
    xmlHttp.send(str);


    ok, if i use:
    var str = "firstname=ken&lastname=scott";
    is posts the data no problem every time, the thing is that i have alot of form posts on my site and would like to use a dynamic collection method.

    PHP Code:
    function FormCollection(theform){

        
    querystring='';

        for (var 
    0i<theform.elements.lengthi++) {
            if(
    i==0){
                
    querystring += theform.elements[i].name "=" theform.elements[i].value;
            } else {
                
    querystring += "&" theform.elements[i].name "=" theform.elements[i].value;
            }
        }

        
    document.getElementById("result").innerHTML querystring// for testing only

    which will build a string like this: "firstname=ken&lastname=scott" from my current form, the problem is, that it only actually added the string to the date when i staticly enter the data, when i use :
    var str = FormCollection(strForm);
    it just posts an empty record. yet it return the correct string with i write it out to a <div>. i was hoping that someone here might have some idea what i can do to make this work..

    hoping this is the correct forum for this issue.

    thanks in advance for your time!
    -Ken

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    Hi Ken,

    I would suggest trying out prototype (http://prototype.conio.net/) which will go through all element on your form and serialise them ready for submission. It's something like

    var str = Form.serialize($("my_form"));

    and that sends everything from the form. It really is awesome!

    to answer your problem, I believe you need to put a return in the function.... so it would be

    Code:
    function FormCollection(theform){
    
        querystring='';
    
        for (var i = 0; i<theform.elements.length; i++) {
            if(i==0){
                querystring += theform.elements[i].name + "=" + theform.elements[i].value;
            } else {
                querystring += "&" + theform.elements[i].name + "=" + theform.elements[i].value;
            }
        }
    
        document.getElementById("result").innerHTML = querystring; // for testing only
    
    //send back the querystring to the var
    return querystring;
    
    }

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    lol,, I'm a retard, i just noticed that too;-)

    thanks! i'll check out prototype.

    cheers,
    -Ken


  •  

    Posting Permissions

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