www.webdeveloper.com
Results 1 to 2 of 2

Thread: passing form name & field to function

Hybrid View

  1. #1
    Join Date
    Jul 2010
    Location
    Birmingham, UK
    Posts
    67

    passing form name & field to function

    I'm sort of new to JS, but I have just managed to get a few simple AJAX applications working. Now that I have the basic down, I want to write a generic function since I'm basically doing the same thing over and over. What I'm trying to do is to pass the name of the form and the specific field name to complete the following line: "var testdata = document.form.input.value;".

    the complete function looks like this:

    Code:
    function checkInput(filename, form, input)
    	{
    	var queryFile = filename + '.php';
    	var getInfo = 'checkuser';
    	var testdata = document.form.input.value;
    	var cache = + new Date().getTime();
    	var url = queryFile + '?' + getInfo + '=' + testdata + '&rand=' + cache;
    	http.open("GET", url, true);
    	http.onreadystatechange = useHttpResponse;
    	http.send(null);
    	}
    If I change the function to include the actual form/field names instead of the variables, then everything works fine. I could, of course, do a series of 'if/else' to test the form name and assign "testdata" the appropriate value, but I would rather try to feed the names through the function to keep it neater and more flexible overall.

  2. #2
    Join Date
    Jul 2010
    Location
    Birmingham, UK
    Posts
    67

    resolved

    i think i've got it now - when i was trying to insert the variable, it was looking like this: "var testdata = document.forms[form].[input].value;". what i discovered (by accident while looking at something else on w3school.com) is that i was putting an extra period between "[form]" and "[input]".

    so the code should look something like this:

    Code:
    function checkInput(filename, form, input)
    	{
    	var queryFile = filename + '.php';
    	var getInfo = 'checkuser';
    	var testdata = document.forms[form][input].value;
    	var cache = + new Date().getTime();
    	var url = queryFile + '?' + getInfo + '=' + testdata + '&rand=' + cache;
    	http.open("GET", url, true);
    	http.onreadystatechange = useHttpResponse;
    	http.send(null);
    	}

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles