开发者

send dynamically created input values to PHP using get request

i have a form. i'm allowing the user to add extra inputs by pressing a button:

$('#add_multi_item').click(function(){

                $('<input />').attr({'type':'text', 'id':'response_input_' + next_add_id}).appendTo('#multi_responses');
                next_add_id++;
            });

and my form is being submitted like this:

$("#add_form").bind('submit', function(event){ 
         event.preventDefault();
         $.get('file.php', {poll_type_select: $("#poll_type_select option:selected").val(), question_input:开发者_如何学JAVA escape($("#question_input").val()), type: "add_poll", user_id:user_id}, function(data) {
                    $('#added').show().html(data);
                    $('#added').hide(7000);
                    $('#question_input').val('');
                    loadList();
                });
    });

how do i add those dynamically created input values into the get request? i guess some people use name arrays for post, but can i do something similar? also i think i will have to escape() all the inputs before sending them to prevent errors when adding to the database.

one option i was thinking was to send a single value, a delimited string created using something like:

$("#multi_responses input").each(string+=escape($(this).val())+"delimiter");

but i don't know if that's the best way. and if i do that, what is a typical delimiter to use?

thanks.


That's right, you just have to give those inputs a name attribute, like:

$('<input />').attr({'type':'text', 'id':'response_input_' + next_add_id, 'name': 'response_input[' + next_add_id + ']'}).appendTo('#multi_responses');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜