Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a problem with checkbox array

this is my code

<?php

include('config.php');

 if(isset($_POST['submit']))
 {
    for($x = 0;$x <= 5;$x++)
    {
        if(isset($_POST['check'][$x]))
        {
            echo "THERE IS A VALUE<br>";
        }
        else{
            echo "EMPTY<br>";
        }
    }
}


?>

<html>
<head>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $("#add-file-field").click(function(){
            $("#text").append("<div class='added-field'><input      type='checkbox' name='check[]' />Never Expired</div>");

        });
    });
</script>
</head>
<body>
<form enctype="multipart/form-data" action="" method="POST">
    <div id="text">
        <input type="checkbox" name="check[]"  />Never Expired<br>
    </div>
    <input type="button" id="add-file-field" name="add" value="Add input field" />
    <input type="submit" value="Upload File" name="submit" id="submit" />
</form>
</body>
</html>

in here i make example i create 5 checkbox

Checkbox1 Checkbox2 Checkbox3 Checkbox4 Checkbox5

and when i check Checkbox3

the output result is

THERE IS A VALUE EMPTY EMPTY EMPTY EMPTY

I want the result is like this

EMPTY EMPTY THERE IS A VALUE EMPTY EMPTY

how to make it like that ? please help me

share|improve this question
    
use my code. This is helpful. Thanks –  Shafiqul Islam Apr 12 at 5:28

4 Answers 4

up vote 1 down vote accepted

You need to add the key to the array

<input type="checkbox" name="check[0]"  />

and

var num = 1;
$("#add-file-field").click(function(){
    $("#text").append("<div class='added-field'><input type='checkbox' name='check["+num+"]' />Never Expired</div>");
    num++;
});
share|improve this answer

Checkboxes that are not checked are not sent to the server by the browser, and hence are not in the $_POST['check'] array. This is why THERE IS A VALUE always comes first.

share|improve this answer

You can see this for a guide in the future...

http://www.html-form-guide.com/php-form/php-form-checkbox.html

Your problem is , you just check that is is set. You must check thier value..

Fix your html form like this

    <input type="checkbox" name="check" value="1"> checkbox1</input>
   "----do the same---------------------------"2""---------2------->"//do this until you get your five checkbox

Then on php

  <?php

    include('config.php');

    if(isset($_POST['submit']))
    {
      for($x = 0;$x <= 5;$x++)
       {
         if($_POST['check']==strval($x))
          {
            echo "THERE IS A VALUE<br>";
          }
         else{
            echo "EMPTY<br>";
          }
       }
    }
 ?>

Hope it works

PS. If anyone see errors please edit it.

share|improve this answer

Hope this is work add

<input type="checkbox" name="check[0]"  />

then edit this

var x = 1;
$("#add-file-field").click(function(){
$("#text").append("<div class='added-field'><input type='checkbox' name='check["+x+"]'     />Never Expired</div>");
x++;
});

then simple php code

 if(isset($_POST['submit']))
{
   // $x = cout($_POST['check']);
    for($x = 0;$x <= 5;$x++)
    {
        if(strlen($_POST['check'][$x])>1)
        {
            echo "THERE IS A VALUE<br>";
        }
        else{
            echo "EMPTY<br>";
        }
    }
}

thanks

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.