www.webdeveloper.com
Results 1 to 8 of 8

Thread: entering form data into array

  1. #1
    Join Date
    Feb 2005
    Posts
    18

    entering form data into array

    Hi there. I have the code below for a validating and retrieving the data from a form. However, i have tried putting the data from the form into an array in the javascript, but am not sure how to do this. Any help would be much appreciated.



    <Script Language=JavaScript>

    function validEntries(isForm){

    isValid = false;
    nButtons = document.getElementsByName('optS_Class');
    for (i=0; i<nButtons.length; i++)
    {
    if (nButtons[i].checked == true){className = nButtons[i].value;isValid = true}
    }

    if (isForm.txtAuthorName.value === ""){alert('Please Enter The Authors Name')}
    else if (isForm.txtPaperId.value == ""){alert('Please Enter The Paper ID')}
    else if (isForm.txtTitle.value == ""){alert('Please Enter Paper Title')}
    else if(isForm.txtTelNo.value ==""){alert('Please Enter Your Telephone No')}
    else if(isForm.txtAddress.value ==""){alert('Please Enter Your Address')}
    else if(isForm.txtPosition.value ==""){alert('You must choose your position ')}
    else if (!isValid){alert('You must choose if you are a IEE Member ')}

    else

    {
    alert("Author Name is: " + isForm.txtAuthorName.value + "." +"\n"+
    "Paper ID is: " + isForm.txtPaperId.value + "." +"\n"+
    "The Title Of The Paper is: " + isForm.txtTitle.value + "." +"\n"+
    "The Telephone Number is: " + isForm.txtTelNo.value + "." +"\n"+
    "Your Address is: " + isForm.txtAddress.value + "." +"\n"+
    "Your Position is: " + isForm.txtPosition.value + "." +"\n"+
    "Your Fax Number is: " + isForm.txtFax.value + "." +"\n"+
    "Your Email address is: " + isForm.txtEmail.value + ".")

    }
    }


    </Script>

  2. #2
    Join Date
    Sep 2004
    Location
    Midwest
    Posts
    1,629
    Shaun_44:

    By way of example:

    <HTML>
    <Head>
    <Script Language=JavaScript>

    var dataArray = new Array();

    function validEntries(isForm){
    n=0;
    nElements = isForm.length;
    for (i=0; i<nElements; i++)
    {
    if (isForm[i].type == 'text')
    {
    dataArray[n++] = isForm[i].value;
    }

    }
    alert(dataArray);
    isForm.submit();
    }

    </Script>
    </Head>
    <Body>
    <Form name='Form1'>
    1st Field:<input type=text name='text1' size=5><br>
    2nd Field:<input type=text name='text2' size=5><br>
    3rd Field:<input type=text name='text3' size=5><br>
    <input type=button value='Submit' id=submit1 onClick="validEntries(this.form)">
    </Form>
    </form>
    </Body>
    </HTML>

  3. #3
    Join Date
    Sep 2004
    Location
    Midwest
    Posts
    1,629
    My previous post has been edited.

  4. #4
    Join Date
    Feb 2005
    Posts
    18
    so is there no way that i can add to the code that i have already, or do i need to start again? The code that I already have displays the data exactlky as i need, it just needs to be placed in an array, purely to show that it can be done...
    Many Thanks
    Last edited by Shaun_44; 02-28-2005 at 02:34 PM.

  5. #5
    Join Date
    Sep 2004
    Location
    Midwest
    Posts
    1,629
    Add to the code you already have?

    That's what my example does.

    Every input type text value is put into an array, just as you asked.

    What is it that you don't understand, about this?:

    var dataArray = new Array();

    function validEntries(isForm){

    n=0;
    nElements = isForm.length;
    for (i=0; i<nElements; i++)
    {
    if (isForm[i].type == 'text')
    {
    dataArray[n++] = isForm[i].value;
    }
    }
    alert(dataArray);

    isValid = false;
    nButtons = document.getElementsByName('optS_Class');
    for (i=0; i<nButtons.length; i++)
    {
    if (nButtons[i].checked == true){className = nButtons[i].value;isValid = true}
    }

    if (isForm.txtAuthorName.value === ""){alert('Please Enter The Authors Name')}
    else if (isForm.txtPaperId.value == ""){alert('Please Enter The Paper ID')}
    else if (isForm.txtTitle.value == ""){alert('Please Enter Paper Title')}
    else if(isForm.txtTelNo.value ==""){alert('Please Enter Your Telephone No')}
    else if(isForm.txtAddress.value ==""){alert('Please Enter Your Address')}
    else if(isForm.txtPosition.value ==""){alert('You must choose your position ')}
    else if (!isValid){alert('You must choose if you are a IEE Member ')}

    else

    {
    alert("Author Name is: " + isForm.txtAuthorName.value + "." +"\n"+
    "Paper ID is: " + isForm.txtPaperId.value + "." +"\n"+
    "The Title Of The Paper is: " + isForm.txtTitle.value + "." +"\n"+
    "The Telephone Number is: " + isForm.txtTelNo.value + "." +"\n"+
    "Your Address is: " + isForm.txtAddress.value + "." +"\n"+
    "Your Position is: " + isForm.txtPosition.value + "." +"\n"+
    "Your Fax Number is: " + isForm.txtFax.value + "." +"\n"+
    "Your Email address is: " + isForm.txtEmail.value + ".")

    }
    }

  6. #6
    Join Date
    Sep 2004
    Location
    Midwest
    Posts
    1,629
    You have 3 successive equal signs here, and it should be two:

    if (isForm.txtAuthorName.value === ""){

  7. #7
    Join Date
    Feb 2005
    Posts
    18
    Sorry, wasnt with it when i read the code you posted earlier. Itworks a treat. Thanks for your help!

  8. #8
    Join Date
    Sep 2004
    Location
    Midwest
    Posts
    1,629
    Shaun:
    You're welcome. I appreciate your courtesy. Take care.

Thread Information

Users Browsing this Thread

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

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