...

View Full Version : this.elements not working in ajax



marf
06-07-2007, 05:15 PM
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



...
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.

GJay
06-07-2007, 07:19 PM
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.

marf
06-07-2007, 07:42 PM
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



<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>

marf
06-07-2007, 09:39 PM
ok forget everything I said above as I have narrowed down the problem.

I use this to call the javascript



<form action="add_user.php" onsubmit="return formSubmit(this, 'add_user.php');" method="POST">


and then look below




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?

GJay
06-08-2007, 08:10 AM
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?

marf
06-08-2007, 01:41 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum