Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an Array with PHP

Tags:

html

php

I have a form with multiple text inputs that all have the same name. How would I process that with my PHP when the user submits the form?

HTML:

<input type="text" name="interest"/>
like image 555
user3259283 Avatar asked Nov 30 '25 20:11

user3259283


2 Answers

I've assumed you're using POST.

You would use

<input type="text" name="interest[]">

Then on the post page, you could use:

foreach($_POST['interest'] as $i){
    echo $i. "<br>";
}

or whichever method you wanted to use to get the POST data.

You could also do something like:

<input type="text" name="interest[music]"/>
<input type="text" name="interest[food]"/>

You can then call this data by using:

<?php echo $_POST['interest']['music']; ?>
like image 153
Albzi Avatar answered Dec 03 '25 10:12

Albzi


<input type="text" name="interest[]"/>
like image 34
Daan Avatar answered Dec 03 '25 12:12

Daan



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!