0

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

1
  • use my code. This is helpful. Thanks Commented Apr 12, 2014 at 5:28

4 Answers 4

1

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++;
});
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

0

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

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.