jQuery Getting an array of form field names and their values

is it possible to target a form and pull an array of input field names and their corresponding values?

below is as far as I've been able to get.
var values = [];
    $("#"+test+" input").val().each(function(index, value) { 
  values[$(this).attr('name')] =  $(this).val();
  alert(index + ': ' + value); 
});

Open in new window


then I want to be able to use jQuery's ajax function to sent to a php script... haven't gotten that far yet.

one of my concerns is, will the strings being pulled from the form fields need to be encoded in any way? remove special charters and so-forth?

i'm just learning jQuery and spreading my wings with JS so i'd rather not use a plug-in if can can help it.

anybody have any ideas?
LVL 2
CybervanesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kiran SonawaneProject LeadCommented:
You can use jquery "serialize" method to target form elements.
Look at this http://api.jquery.com/serialize/
0
StingRaYCommented:
Try this.

var values = [];
$("#"+test+" input").each(function(index, value) { 
    values[$(this).attr('name')] =  $(this).val();
    alert(index + ': ' + value); 
});

Open in new window


Anyway, serialization using serialize method would be a better approach to your problem.
0
chaitu chaituCommented:
we can do like this.

$("input:text,select,input[type=checkbox],input[type=radio]", ".information").each(function(el) {
				if($(this).attr("type") == "checkbox" || $(this).attr("type") == "radio")
				{
					if($(this).is(':checked'))
					{	
					  values[$(this).attr("name")] = $(this).val();
					}
				}
				else
				{
					values[$(this).attr("name")] = $(this).val();
				}
		});

	 }
	
	$.ajax({
		 type: 'POST',
		 url:  url,
		 data: values,
		 success: function(msg){ 		 
			
		}  
	});

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.