www.webdeveloper.com
Results 1 to 2 of 2

Thread: Cycle Through form Inputs and match with Json array

  1. #1
    Join Date
    Jul 2009
    Posts
    56

    Cycle Through form Inputs and match with Json array

    I run an ajax request and return a json array.
    There is more in the json array than there is fields on the page. I want to cycle through text fields and find their NAME and match that to the json array and fill in the value.

    The names of the fields and the names in the json array are the same.

    Code:
    <script type="text/javascript">
    function loadIntake(){
    	var client = <?php echo $id ?>;
    	
    	$.post("loadIntake.php", 
    		{id: client},
    		function(jdata){
    				$('input[type=text]').attr("name", function(n, idata){
    					//$(idata).val(jdata.idata);
    					alert (jdata.idata);
    				})
    		},
    		"json"
    	);  
    
    }
    </script>
    What am I doing wrong here?

  2. #2
    Join Date
    Jul 2009
    Posts
    56
    I was able to just dump the json array to all fields doing this:

    Code:
    <script type="text/javascript">
    function loadIntake(){
    	var client = <?php echo $id ?>;
    	
    	$.post("loadIntake.php", 
    		{id: client},
    		function(jdata){
    			$.each(jdata, function(i, data) {
    				$('#' + i).val(data);
    			});
    				$('input[type=text]').attr("name", function(n, idata){
    					//$(idata).val(jdata.idata);
    					//alert (idata);
    				})
    		},
    		"json"
    	);  
    
    }
    </script>
    However, some of the fields are text fields, some are textfields, some are radio buttons, and checkboxes. As I said the json array has MORE fields than there are fields on the page. So I dont want to deal with unnecessary code runs.

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