0

I have this HTML form:

<form>
    <div>
        <h1>Item 1</h1>
        <input name="item_title[]" value="Any 1">
        <input name="phone[]" placeholder="phone number">
        <input name="phone[]" placeholder="phone number">
        <input name="phone[]" placeholder="phone number">
    </div>
    <div>
        <h1>Item 1</h1>
        <input name="item_title[]" value="Any 2">
        <input name="phone[]" placeholder="phone number">
        <input name="phone[]" placeholder="phone number">
        <input name="phone[]" placeholder="phone number">
    </div>
    <div>
        <h1>Item 3</h1>
        <input name="item_title[]" value="Any 3">
        <input name="phone[]" placeholder="phone number">
        <input name="phone[]" placeholder="phone number">
        <input name="phone[]" placeholder="phone number">
    </div>
    ...
</form>

On server side, in PHP for example I can loop through item_title[] to know each item:

foreach($_POST['item_title'] as $key => $value){
    $item_order = $key; // or $key + 1
    $item_title = $value;
}

my problem is that I get a item_title array with 3 elements and a phone array will 9 elements but what I really want is to get:

item_title: phone1, phone2, phone3 // 1
item_title: phone1, phone2, phone3 // 2
item_title: phone1, phone2, phone3 // 3

so I can loop each time on item_title array and get the 3 phone numbers respectively to the items.

I hope my idea is clear :/

Thank you!

3 Answers 3

1

If you can index your inputs, you can use multidimensional arrays:

<input name="item_title[0]" value="Any 1">
<input name="phone[0][]" placeholder="phone number">
<input name="phone[0][]" placeholder="phone number">
<input name="phone[0][]" placeholder="phone number">

And process them in php:

foreach($_POST['item_title'] as $key => $value){
    $item_title = $value;
    // Output formatted as in your example, but you can loop the array
    echo $item_title . ': ' . implode(',', $_POST['phone'][$key]);
}

Hope it helps

Sign up to request clarification or add additional context in comments.

1 Comment

this is exactly what i want! Thank you.
1

A solution would be that you create seperate phone names like this :

<div>
    <h1>Item 2</h1>
    <input name="item_title[]" value="Any 2">
    <input name="phone1[]" placeholder="phone number">
    <input name="phone2[]" placeholder="phone number">
    <input name="phone3[]" placeholder="phone number">
</div>
<div>
    <h1>Item 1</h1>
    <input name="item_title[]" value="Any 1">
    <input name="phone1[]" placeholder="phone number">
    <input name="phone2[]" placeholder="phone number">
    <input name="phone3[]" placeholder="phone number">
</div>

Comments

0

You can use seelct2 check multiple select box option. For inserting multiple value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.