Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing dynamic input field array item values with different names to php

Tags:

arrays

forms

php

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!

like image 475
tschortsch Avatar asked May 10 '26 20:05

tschortsch


1 Answers

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.

like image 109
AleOtero93 Avatar answered May 13 '26 11:05

AleOtero93



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!