0

I have this code here:

$.post('search_results.php', { name: form.name.value },

It helps me pass one value from an input field. Now I have a <select> </select> and I need to pass it too, I need to pass more variables.

How should I do it?

Something like:

$.post('search_results.php', { name: form.name.value , select: form.select.value },
0

4 Answers 4

2

Assuming all the elements are in one form, the easiest way is to use .serialize:

$.post('search_results.php', $('#formID').serialize());

You can even just select the form control elements you want, for example:

$('input[name="name"], select[name="gender"]').serialize()
Sign up to request clarification or add additional context in comments.

2 Comments

could you please tell me how to use it n the above way? i realize this is simpler, but until i make it work, it will take em some time
I don't you know what you mean. Of course you can use { name: form.name.value , select: form.select.value } if you want to, but I assumed you ask for an easier solution, otherwise, why would you ask at all?
0

I usually create a data object and then perform a loop that gathers up all values and sticks them into the object, then send it to the server.

You can use something like this:

var data = {};

$('form *[name]').each(function() {
   data[$(this).attr('name')] = $(this).val();
});

$.post('search_results.php', data);

Comments

0

refer jquery serialize() method. you will find examples for how to achieve it

http://api.jquery.com/serialize/

Comments

0

you can use serialize() method just above said or if you struggle, you can use key:value pairs,

$.post('search_results.php', { ('firstname': form.firstname.value , 'password': form.password.value) }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.