homepage Welcome to WebmasterWorld Guest from 54.211.213.10
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Getting Form Parameters
both names and corresponding values
rover




msg:3629389
 6:39 pm on Apr 17, 2008 (gmt 0)

I need to access both the form input field names and the values using javascript.

I know I can get the values with something like:

var test_var = form.fieldname.value

But I am having forms created dynamically with various number of fields, field names, etc. and I'm hoping there is a way to use javascript to get a listing for both the actual field names and their corresponding values entered by the user.

Does anyone know if there is a way to get an array or something like this with both the field names and the corresponding values entered on the form?

 

rover




msg:3629441
 7:31 pm on Apr 17, 2008 (gmt 0)

O.k. I found some ideas, and I did find a way that I could access the corresponding values through something like this using an id value the same as the field name value. In case it is helpful to anyone else:

<form id="myForm">
Firstname: <input name="fname" id="fname" type="text" value="Mickey" />
Lastname: <input name="lname" id="lname" type="text" value="Mouse" />
<input id="sub" type="button" value="Submit" />
</form>

<p>Get the name and value of all the elements in the form:<br />
<script type="text/javascript">
var x=document.getElementById("myForm");
for (var i=0;i<x.length;i++)
{
document.write(x.elements[i].id);
document.write(" --> ");
document.write(x.elements[i].value);
document.write("<br />");

document.write("<br />");
}
</script>
</p>

elitebomber




msg:3630083
 4:40 pm on Apr 18, 2008 (gmt 0)

I have a problem with AJAX and FireFox that is on topic with form values.

var extra = document.form1.Students.value;

I'm using that in my AJAX function to get the value of a list/menu called Students. Then the AJAX sends the value to a php file and the php file recreates the form and removes the value that they previously selected.

No page refresh has taken place since it was all AJAX. The user then selects another name from the list/menu and the function is called again but this time var extra is returning the old value and not the new one. I need some way of refreshing Students.value. I'm not having this problem in IE.

elitebomber




msg:3633460
 6:39 pm on Apr 23, 2008 (gmt 0)

I was able to fix my problem using window.location.reload(); at the end of my AJAX call. For whatever reason this wasn't required in IE. I'm guessing that IE automatically reloads when XMLHttpRequestObject returns something. Or maybe the implementation of form values is just totally different. Anyone know?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved