-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"/>
3
  • 1
    Where is your code? Commented Dec 2, 2020 at 17:34
  • Sorry, I will edit right now Commented Dec 2, 2020 at 17:35
  • 1
    You don't have a name attribute in this inputs Commented Dec 2, 2020 at 18:12

2 Answers 2

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

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.