1

I have a form like below:

<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>

How to get value of this form in php $_Post because the name of fields are like <br/>name="book[0].title"**<br/>?

and there are many dynamic different name input fields.

4
  • see print_r($_POST). Commented May 15, 2015 at 9:54
  • As mentioned @panther, you can use either print_r() or var_dump() to visualize data and you'll see that "aggregate fields" will be managed as arrays Commented May 15, 2015 at 9:57
  • And you should be careful with dots in HTML tags name ! In the use of $POST[...] in PHP, you need to note this PHP rule: “Dots and spaces in variable names are converted to underscores. For example <input name="a.b" /> becomes $_REQUEST["a_b"].” See php.net/manual/en/language.variables.external.php Commented May 15, 2015 at 10:02
  • Are we not supposed to answer queries here? Commented May 15, 2015 at 10:04

3 Answers 3

2

If you can use for multiple books then use book[0].title like below,

<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> 
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to use same form then change name of all textbox like,

    <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="submit" class="btn btn-default addButton"><i class="fa fa-plus"></i></button>
    </div>
</div>

And get value of this form like echo $_POST['book'][0]['title'];

I hope this can help you.

1 Comment

Sir How to loop this type of input array values
0

You need to change name attribute,

 <div class="col-xs-4">
    <input type="text" class="form-control" name="book_title" placeholder="Title" />
</div>

book[0].title, this is not valid name.

You can now get something like this,

$_POST['book_title']

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.