3

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"/>
3
  • I love how I got an immediate -4 for trying to answer a question like this. Commented Feb 27, 2014 at 14:53
  • @NobleUplift That's rough, but 1) since it's deleted most people won't be able to see it, and 2) not really relevant here, so someone will probably eventually flag your comment and it (and this) will likely be removed for being too meta. Commented Feb 27, 2014 at 14:55
  • @NobleUplift Reading the comments it looks as though you got the answer incorrect the first time round though? Commented Feb 27, 2014 at 14:56

4 Answers 4

5

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

3 Comments

Don't you need quotation marks around interest[]?
Thanks @BeatAlex for the improvement(s); this was already a good answer. Worth each upvote!
Hyperlink for you, because citations are cool: php.net/manual/en/faq.html.php#faq.html.arrays
2
<input type="text" name="interest[]"/>

2 Comments

Add some PHP code and better explain your answer to the OP.
-1 This answer desperately needs an explanation, a hyperlink, something to help OP know what the magic square braces do.
2

You should add square brackets. This triggers PHP to put them in an array like this:

HTML

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

PHP

//Get values
var_dump($_POST['interest']);

Comments

0

Use brackets in your input field to create an array of POST values:

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

<?php
var_dump($_POST['interest']); // will be an array
?>

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.