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 6 of 6
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    this.elements not working in ajax

    Anybody know if theres something special to get this.elements[0] working? I basically have my ajaxhttpreq.js which the function in that file has a for loop like

    Code:
    ...
    for(var i=0;i<thisForm.elements.length;i++){
    			if(thisForm.elements[i].type == "text"){
    				formData = formData + thisForm.elements[i].name + "=" + escape(thisForm.elements[i].value) + "&";
    }
    ...
    but formData is ALWAYS empty, no matter what. Why is this?

    I call this function with onSubmit = "return ajaxreq(this,"myfile.php");" in my <form> tag so that looks legit.

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    you're going to need to show more code, it's not clear what 'this' is in the code you've pasted.
    Your explanation mentions 'this.elements', but the code has thisForm.elements. That's a little confusing, more code should clarify.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    New Coder
    Join Date
    Feb 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function ajaxreq(thisForm, pageURL){
    
    for(var i=0;i<thisForm.elements.length;i++){
    			if(thisForm.elements[i].type == "text"){
    				formData = formData + thisForm.elements[i].name + "=" + escape(thisForm.elements[i].value) + "&";
    }
    return false;
    }
    There... That doesn't work. It doesn't even get into the for loop because aparrently thisForm.elements.length must = 0 or null. But why, because my form is legit... this is what it looks like

    Code:
    <form action="add_user_submit.php" onsubmit="return ajaxreq(this,'add_user_submit.php');" method="POST">
    <table width="400" border="0" cellspacing="2" cellpadding="0">
      <tr>
        <td width="100" class="cell0">Name: </td>
        <td width="300"><input type="text" name="name" size="10" value="" /></td>
      </tr>
      <tr>
        <td width="100" class="cell1">country: </td>
        <td width="300"><input type="text" name="country" value="" size="10" /></td>
      </tr>
      <tr>
        <td width="100" class="cell0">birthdate:</td>
        <td width="300"><input type="text" name="birthdate" id="2" maxlength="50" value="" /></td>
      </tr>
    </table>
    <input type="submit" name="submit" value="Add User" />
    </form>

  • #4
    New Coder
    Join Date
    Feb 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok forget everything I said above as I have narrowed down the problem.

    I use this to call the javascript

    Code:
    <form action="add_user.php" onsubmit="return formSubmit(this, 'add_user.php');" method="POST">
    and then look below

    Code:
    function formSubmit(thisForm, pageURL){
       alert(thisForm.elements[0].value); //THIS WORKS AND Shows the Value
    
       secondFunction(thisForm);
    return false;
    }
    
    function secondFunction(thisForm){
       alert(thisForm.elements[0].value); //THIS Doesn't show, the alert window shows undefined
    
    }
    So why does it not show in the second function but in the first one its fine? I tried assigning it to a variable in the first one and passing and still no go... any ideas?

  • #5
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    are you using IE? (does it work in firefox?) do you have an element with an id of 'thisForm'? what happens if you change the variable name 'thisForm' to something completely different?
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #6
    New Coder
    Join Date
    Feb 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    still no go in IE or Firefox. Additionally I tried doing var thisForm2 = thisForm, and then secondFunction(thisForm2); still no dice....

    any ideas? This has to be some stupid syntax, or logic error


  •  

    Posting Permissions

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