3

I'm using this form from formvalidation.io:

<form id="bookForm" method="post" class="form-horizontal">
<div class="form-group">
    <label class="col-xs-1 control-label">Book</label>
    <div class="col-xs-4">
        <input type="text" class="form-control" name="book[0].title" placeholder="Title" />
    </div>
    <div class="col-xs-4">
        <input type="text" class="form-control" name="book[0].isbn" placeholder="ISBN" />
    </div>
    <div class="col-xs-2">
        <input type="text" class="form-control" name="book[0].price" placeholder="Price" />
    </div>
    <div class="col-xs-1">
        <button type="button" class="btn btn-default addButton"><i class="fa fa-plus"></i></button>
    </div>
</div>

The input field names are very special, e.g. name="book[0].title".

How to pass all these input field array values to my php script?

I guess the "." is not php compliant. Changing the names in a way like name="book[0][title] leads to error messages like Warning: Illegal string offset 'title' in www/... with print_r($_POST['book'][0]['title']);.

I appreciate any help!

2 Answers 2

1

You have to use print_r($_POST[books][0][title]); instead, hope it helps. Also, is it 'book' or 'books'? That could bring an error too. But my point is that you don't have to use ' delimiter.

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

3 Comments

Sorry for my typo! It is 'book'.
How to loop this type of input array values in php?
Something like foreach($_POST[book] as $myBook){ ... }
1

Below format should work

<input type="text" class="form-control" name="book[0][title]" placeholder="Title" />

When you print_r($_POST) you will get the submitted data in proper array format. And you can also get the title by using $_POST['book'][0]['title']

[book] => Array
        (
            [0] => Array
                (
                    [title] => titletext
                    [isbn] => isbntext
                    [price] => 100
                )

        )

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.