Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. Javascript - Dynamic arrays

Javascript - Dynamic arrays

Scheduled Pinned Locked Moved Web Development
helpjavascriptphpdata-structurestools
5 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bryant May
    wrote on last edited by
    #1

    Hi everyone, Im hoping someone can help me out here before I have a nervous breakdown... Im trying to create a dynamic array of items which have been passed through from a php form. The script is designed to check that each of the form fields have been completed, return an error message if they havent and then enter them in to an array. The array is to then be passed to another PHP script for other things to be done to it. The part Im stuck on is the creation of the array key. This needs to be done in a way that allows me to add the form field name as the key and the value of the form field as the array value so something like: "array[firstname] = Bryant;" At the moment I have this all being done in the middle of a for loop which is iterating through the form fields to see if they have been completed. The code for this is below.

    function submitForm(form, messageDiv, action)
    {
    var targetDiv = document.getElementById(messageDiv);
    var inputs = document.forms[form].getElementsByTagName("input");
    var selects = document.forms[form].getElementsByTagName("select");
    var fieldName = document.getElementsByTagName("label");
    var length = selects.length + inputs.length;

    var data = new Array();
    
    for(i=0; i < length; i++)
    {			
    	if (inputs\[i\]) 
    	{
    		if (inputs\[i\].value == "")
    		{
    			targetDiv.innerHTML = inputs\[i\].name+" is missing";
    			var error = 1;
    			return;						
    		}
    		else
    		{
    			var error = 0;
    			var field = inputs\[i\].name;
    		        var value = inputs\[i\].value;
    		        data\[field\] = value;				
    		}
    		
    	}	
    	
    	if (selects\[i\])
    	{
    		if (selects\[i\].value == "")
    		{
    			targetDiv.innerHTML = selects\[i\].name+" is missing";
    			var error = 1;
    			return;
    		}
    		else
    		{
    			var error = 0;
    			var field = selects\[i\].name;
    			var value = selects\[i\].value;
                                data\[field\] = value;
    		}	
    		
    	}
    }	
    	if (error == 0)
    	{
    		targetDiv.innerHTML = "";
    		saveForm(form, messageDiv, data);
    	}				
    

    }

    I've managed to half do this by using a numerical value for the key but I really need to have the form field name as the key so that I can associate the form field with the data when I pass it to the php script. Having said that when I use numeric keys on the second half is completed (the selects) becuause the inputs are all over written because the array key isnt incremented until the next iteration of the for loop.

    I would really apprciate some guidance on this, Im kinda new to JavaScr

    F 1 Reply Last reply
    0
    • B Bryant May

      Hi everyone, Im hoping someone can help me out here before I have a nervous breakdown... Im trying to create a dynamic array of items which have been passed through from a php form. The script is designed to check that each of the form fields have been completed, return an error message if they havent and then enter them in to an array. The array is to then be passed to another PHP script for other things to be done to it. The part Im stuck on is the creation of the array key. This needs to be done in a way that allows me to add the form field name as the key and the value of the form field as the array value so something like: "array[firstname] = Bryant;" At the moment I have this all being done in the middle of a for loop which is iterating through the form fields to see if they have been completed. The code for this is below.

      function submitForm(form, messageDiv, action)
      {
      var targetDiv = document.getElementById(messageDiv);
      var inputs = document.forms[form].getElementsByTagName("input");
      var selects = document.forms[form].getElementsByTagName("select");
      var fieldName = document.getElementsByTagName("label");
      var length = selects.length + inputs.length;

      var data = new Array();
      
      for(i=0; i < length; i++)
      {			
      	if (inputs\[i\]) 
      	{
      		if (inputs\[i\].value == "")
      		{
      			targetDiv.innerHTML = inputs\[i\].name+" is missing";
      			var error = 1;
      			return;						
      		}
      		else
      		{
      			var error = 0;
      			var field = inputs\[i\].name;
      		        var value = inputs\[i\].value;
      		        data\[field\] = value;				
      		}
      		
      	}	
      	
      	if (selects\[i\])
      	{
      		if (selects\[i\].value == "")
      		{
      			targetDiv.innerHTML = selects\[i\].name+" is missing";
      			var error = 1;
      			return;
      		}
      		else
      		{
      			var error = 0;
      			var field = selects\[i\].name;
      			var value = selects\[i\].value;
                                  data\[field\] = value;
      		}	
      		
      	}
      }	
      	if (error == 0)
      	{
      		targetDiv.innerHTML = "";
      		saveForm(form, messageDiv, data);
      	}				
      

      }

      I've managed to half do this by using a numerical value for the key but I really need to have the form field name as the key so that I can associate the form field with the data when I pass it to the php script. Having said that when I use numeric keys on the second half is completed (the selects) becuause the inputs are all over written because the array key isnt incremented until the next iteration of the for loop.

      I would really apprciate some guidance on this, Im kinda new to JavaScr

      F Offline
      F Offline
      fly904
      wrote on last edited by
      #2

      I wrote this on the fly.

      function checkForm(formID)
      {
      var data = new Array();
      var form = document.getElementById(formID);
      for (var i = 0; i < form.elements.length; i++)
      {
      if(form.elements[i].value != "")
      data[form.elements[i].name] = form.elements[i].value;
      else
      return 0;
      }
      return data;
      }

      It "should" work for both input and select which will save you the hastle of doing each seperatly :)

      hmmm pie

      B 1 Reply Last reply
      0
      • F fly904

        I wrote this on the fly.

        function checkForm(formID)
        {
        var data = new Array();
        var form = document.getElementById(formID);
        for (var i = 0; i < form.elements.length; i++)
        {
        if(form.elements[i].value != "")
        data[form.elements[i].name] = form.elements[i].value;
        else
        return 0;
        }
        return data;
        }

        It "should" work for both input and select which will save you the hastle of doing each seperatly :)

        hmmm pie

        B Offline
        B Offline
        Bryant May
        wrote on last edited by
        #3

        Hi there - cheers for getting back to me. The form checking that you provided is awesome, and much quicker than the way I was going to be doing it!!! Unfortunately the array bit doesnt work. Im returning the 'data' array to another function and when I try to alert the contents it comes up with nothing.....no matter how I try to get at the content. Presumably the array should contain my seven form field values along with the relevant field name keys but I cant get at any of it. No errors are thrown and eveything looks like it should be working. Any ideas? Again, cheers for getting back to me so quickly! ;)

        F 2 Replies Last reply
        0
        • B Bryant May

          Hi there - cheers for getting back to me. The form checking that you provided is awesome, and much quicker than the way I was going to be doing it!!! Unfortunately the array bit doesnt work. Im returning the 'data' array to another function and when I try to alert the contents it comes up with nothing.....no matter how I try to get at the content. Presumably the array should contain my seven form field values along with the relevant field name keys but I cant get at any of it. No errors are thrown and eveything looks like it should be working. Any ideas? Again, cheers for getting back to me so quickly! ;)

          F Offline
          F Offline
          fly904
          wrote on last edited by
          #4

          alert(data) will not work, but alert(data['anExampleFieldName']) will. If you try and go through the array using indexing then it won't work either :( Edit: Have you tried using the Firefox[^] Add-on called Firebug[^] which has a very useful JavaScript debugger.

          hmmm pie

          modified on Tuesday, March 17, 2009 12:07 PM

          1 Reply Last reply
          0
          • B Bryant May

            Hi there - cheers for getting back to me. The form checking that you provided is awesome, and much quicker than the way I was going to be doing it!!! Unfortunately the array bit doesnt work. Im returning the 'data' array to another function and when I try to alert the contents it comes up with nothing.....no matter how I try to get at the content. Presumably the array should contain my seven form field values along with the relevant field name keys but I cant get at any of it. No errors are thrown and eveything looks like it should be working. Any ideas? Again, cheers for getting back to me so quickly! ;)

            F Offline
            F Offline
            fly904
            wrote on last edited by
            #5

            Alternativley if you want to use indexing then you can use a 2d array.

            var data = new Array()

            for (var i = 0; i < LENGTH; i++)
            {
            if(notEmpty)
            {
            data[i] = new Array(2);
            data[i][0] = name;
            data[i][1] = value;
            }
            }

            That will be able to use indexing, although the structure is not what you previously asked for.

            hmmm pie

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups