开发者

three text input into one POST name

If I have three text input and I want to combine the values in these three text input into one POST name, how can I do that?

UPDATE:

a good example would be if I have a phone number field, and I have three fields for the phone number... I wanted thi开发者_JAVA技巧s to be posted as one so back in the server side I can just access it as $POST['phone']

Would be nice if something like jQuery can help me out here.


Have them as an array:

<input type="text" name="inputs[]" />
<input type="text" name="inputs[]" />
<input type="text" name="inputs[]" />

Then you can access them in the POST array.

You have not indicated the programming language, but in PHP it would be, $_POST['inputs'][0], $_POST['inputs'][1], $_POST['inputs'][2]...


Since you want to have only one phone input which contains the full phone number appear on server side, not parts of it, and you are using jQuery in your project, this will make things easy for you:

1. Sample Markup

<form id="my_form" method="post">
    <input type="text" name="phones[]" />
    <input type="text" name="phones[]" />
    <input type="text" name="phones[]" />
    <input type="hidden" name="phone" />
    <input type="submit" name="send" value="Send It" />
</form>

2. jQuery

$(document).ready(function(){
    var $phones = $('#my_form input[name="phones[]"]'),
        $phone = $('#my_form input[name="phone"]');

    $('#my_form').submit(function(){
        // join all the phone parts together
        var phone_number = '';
        $phones.each(function(){
            phone_number += this.value;
        });

        // change the hidden input element's value
        $phone.val(phone_number);

        // remove the phone parts input elements
        $phones.remove();
    });
});


I wonder why you would need to do that.

In php, you can do as Shef said.

In simple single word cases, you can concatenate them with some separator (e.g. # or $) and process the same on the server side.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜