0

I have the following code

<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">

     <label for="name">Name</label><br>
     <input type="text" name="name"></input><br>
     <input type="file" size="60" name="myfile"><br>
     Type 1:<input type="checkbox" name="product[]" value"type1" /><br>
     Type 2:<input type="checkbox" name="product[]" value"type2" /><br>
     Type 3:<input type="checkbox" name="product[]" value"type3" /><br>
     <input type="submit" value="Submit">
 </form>

foreach($_POST["product"] as $value)
            {
                echo $value ;

            }

it should return the values user have selected. But it gives only 'on' as output.

2
  • 2
    Please post more of your code. Also note that your value"type1" is not correctly typed. It should be value="type1" Commented Aug 26, 2014 at 10:02
  • use distinct name instead array for each checkbox Commented Aug 26, 2014 at 10:02

5 Answers 5

2

Set the name in the form to check_list[] and you will be able to access all the checkboxes as an array($_POST['check_list'][]).

An example code:

    <form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">
    <input type="checkbox" name="product[]" value="type 1">
    <input type="checkbox" name="product[]" value="type 2">
    <input type="checkbox" name="product[]" value="type 3">
    <input type="checkbox" name="product[]" value="type 4">
    <input type="checkbox" name="product[]" value="type 5">
    <input type="submit" />
    </form>
    <?php
    if(!empty($_POST['product'])) {
      foreach($_POST['product'] as $check) 
      {
            echo $check; 
      }
    }
    ?>
Sign up to request clarification or add additional context in comments.

Comments

0

your value is wrong,you forgot =

 Type 1:<input type="checkbox" name="product[]"  value="type1" /><br>
 Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
 Type 3:<input type="checkbox" name="product[]" value="type3" /><br>

Comments

0

It must be value="type1" not value"type1". Try this

     <label for="name">Name</label><br>
     <input type="text" name="name"></input><br>
     <input type="file" size="60" name="myfile"><br>
     Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
     Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
     Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
     <input type="submit" value="Submit">
 </form>
<?php

foreach($_POST["product"] as $value)
            {
                echo $value ;

            }

?>

Comments

0

try this

<form id="myForm" action="" method="post" enctype="multipart/form-data">

     <label for="name">Name</label><br>
     <input type="text" name="name"></input><br>
     <input type="file" size="60" name="myfile"><br>
     Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
     Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
     Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
     <input type="submit" value="Submit">
 </form>




<?php

if (isset($_POST)) {
  foreach($_POST["product"] as $value)
            {
                echo $value ;

  }
}

Comments

0

First you forgot the = after value

Remodifying your script becomes this

<form id="myForm" action="upload.php" method="post" enctype="multipart/form-data">

     <label for="name">Name</label><br>
     <input type="text" name="name"></input><br>
     <input type="file" size="60" name="myfile"><br>
     Type 1:<input type="checkbox" name="product[]" value="type1" /><br>
     Type 2:<input type="checkbox" name="product[]" value="type2" /><br>
     Type 3:<input type="checkbox" name="product[]" value="type3" /><br>
     <input type="submit" value="Submit">
 </form>


$check = $_POST["product"]

foreach($check as $value)
            {
                echo $value ;

            }

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.