javascript - Dynamically get value of input fields with jQuery and process them with PHP -
i working on page show different input fields, depending on selection user did.
example 1:
<input type="text" name="color" value="" id="5"> <input type="text" name="location" value="" id="6"> <input type="text" name="age" value="" id="7">
example 2:
<input type="text" name="color" value="" id="5"> <input type="text" name="destination" value="" id="8"> <input type="text" name="hours" value="" id="9"> <input type="text" name="date" value="" id="10">
question 1: how can input fields jquery when input fields dynamic?
question 2: on serverside, want process input fields value , id. how can make dynamic?
i know can done when fix, e.g.:
var color = $('#color').val(); var destination = $("#destination").val(); var datastring = 'color=' + color + "&destination=" + destination; $.ajax({ type: "get", url: "do_something.php", data: datastring, async: false, success: function(data){ console.log('success'); } });
you can use .serialize()
create data-string. dynamically grab data form , turn string you're attempting build:
$.ajax({ type: "get", url: "do_something.php", data: $("form").serialize(), async: false, success: function(data){ console.log('success'); } });
docs .serialize()
: http://api.jquery.com/serialize
note, may need refine $("form")
selection more specific if have more 1 form in dom.
as second question, should keep questions on 1 question per post. being said, should set id
attribute value
attribute instead, way passed php script when form submitted, id
lost since it's not transmitted form.
Comments
Post a Comment