Dirus
Dirus

Reputation: 1043

php checkbox array submit issue

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

Upvotes: 0

Views: 505

Answers (4)

Shafiqul Islam
Shafiqul Islam

Reputation: 5690

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

Upvotes: 0

Poomrokc The 3years
Poomrokc The 3years

Reputation: 1099

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.

Upvotes: 0

Sean
Sean

Reputation: 12433

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++;
});

Upvotes: 1

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

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.

Upvotes: 0

Related Questions