-4

I am new to PHP and I couldn't find something I need. So my problem is that I have 4 inputs in HTML with the same name and I want to make an array with them in PHP. Is there any way to do this? View a picture below: Photo of 4 inputs

So these 4 inputs have same name and I want to get information which user typed in and insert it into HTML page, Please answer my question or recommend a better way to do this!

Here is my code:

   <input class="inputText" type = "text" class="form-control" placeholder="Some text here"/>
   <input class="inputText" type = "text" class="form-control" placeholder="Some text here"/>
   <input class="inputText" type = "text" class="form-control" placeholder="Some text here"/>
   <input class="inputText" type = "text" class="form-control" placeholder="Some text here"/>

2 Answers2

0

use this :

 <input class="inputText" type = "text" class="form-control" name="text[]" placeholder="Some text here" />
       <input class="inputText" type = "text" class="form-control"  name="text[]"  placeholder="Some text here"/>
       <input class="inputText" type = "text" class="form-control" name="text[]"  placeholder="Some text here"/>
       <input class="inputText" type = "text" class="form-control"  name="text[]"  placeholder="Some text here"/>

then you will get the "name" array in your php file when you print $_POST

Hammad Ahmed khan
  • 1,618
  • 7
  • 19
0

You dont have a name in your input, so you'll never get the value in your $_POST :). So, we add names:

<input name="thing1" value="1" />
<input name="thing2" value="2" />
<input name="thing3" value="3" />
<input name="thing4" value="4" />

Now you can use $_POST['thing1'] to $_POST['thing4']. But we want them in an array, so they need to be the same name.

<input name="thing" value="1" />
<input name="thing" value="2" />

Unfortunally, you now have one value in $_POST['thing'], which is '2' (the last overwrites the previous). In order to tell PHP/html to make it an array, you can use the same trick as you'd use in PHP :

<input name="thing[]" value="1" />
<input name="thing[]" value="2" />
<input name="thing[]" value="3" />
<input name="thing[]" value="4" />

And you you have $_POST['thing'] with the value ['1','2','3','4']

Martijn
  • 15,791
  • 4
  • 36
  • 68