Possible to collect multiple values into one PHP varible?

  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

Hello all, I have developed a HTML form that post to a PHP script that inserts values into a MySQL DB. Everything works fine however, I have a group of around 60 check boxes that the user can select 3 from (using JS to set a max). The problem that I am having is I am not sure how to pull these three values from the form. If I add the same name attribute to all 60 selections then only one of the values is sent via the POST method. The only other way i can think to make this work is if I add around 60 different values to my MySQL table.

My question is: Is there a way to collect multiple values with the same name attribute from a html form?

An example of the problem am having is below:

Code: [ Select ]
<?php
if($_POST)
{
$ser = $_POST["sports"];
echo $ser;
}
else
{
?>
<form action="sports.php" method="POST">
Soccer: <input type="checkbox" name="sports" value="soccer"  /><br />
Football: <input type="checkbox" name="sports" value="football"  /><br />
Baseball: <input type="checkbox" name="sports" value="baseball"  /><br />
Basketball: <input type="checkbox" name="sports" value="basketball"  />
<input type="submit" value="Submit" />
</form>
<?php
}
?>
  1. <?php
  2. if($_POST)
  3. {
  4. $ser = $_POST["sports"];
  5. echo $ser;
  6. }
  7. else
  8. {
  9. ?>
  10. <form action="sports.php" method="POST">
  11. Soccer: <input type="checkbox" name="sports" value="soccer"  /><br />
  12. Football: <input type="checkbox" name="sports" value="football"  /><br />
  13. Baseball: <input type="checkbox" name="sports" value="baseball"  /><br />
  14. Basketball: <input type="checkbox" name="sports" value="basketball"  />
  15. <input type="submit" value="Submit" />
  16. </form>
  17. <?php
  18. }
  19. ?>


If a user selects for example soccer and football, only football is echoed. I want for both values to post with out having to change the name attribute. Otherwise I will have add 60 different name attributes, 60 different PHP variables, and 60 different MySQL fields to my table. There has got to be an easier way. Anyone have any ideas?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • RedBMedia
  • Proficient
  • Proficient
  • User avatar
  • Posts: 315

Post 3+ Months Ago

Ok, solution found! Apparently, if you add a "sport[]" value as the name attribute then it forces the values to be parsed as an array when passed through PHP via POST.
  • mattfitz
  • Born
  • Born
  • mattfitz
  • Posts: 1

Post 3+ Months Ago

If you make all the 'name' attributes the same doesn't that invalidate the markup? It also kills the possibility of using the 'label' tag to make active labels since it ties to the name attribute.
I am currently working on a project with far less than 60 checkboxes but I still want an automated solution. I think the best approach is to define the group of checkboxes in a PHP array and write out that part of the web form. The resultant HTML is verbose, but the PHP code to collect the checked boxes is better.
Does anyone have a semantic (X)HTML solution to this?

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 125 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.