0

I have checkboxes whose value attribute is coming from database.I want the user to always check the checkboxes having same value if he does not then alert using javascript.I tried to do this using the following javascript code but no use

<script>
    function ck()
    {
        var x,item,f;
        list=document.getElementById("yo");
        f=list[0].value;
        for(index=1;index<list.length;++index)
        {
            item=list[index];
            x=item.value;
            if(x!=f)
            {
                alert("Wrong type checkboxes selected!");
                break;
            }
        }
    }
</script>

Below is the PHP code for checkbboxes

<?php 
    $flag1=-1;
    $conn = mysqli_connect("localhost", "root","","discvr");
    if(! $conn )
    {
        die('Could not connect: ' . mysql_error());
    }

    for($i = 0; $i < 150; $i++) {
        echo "<tr>";
        $cur_date=date("Y-m-d");
        echo "<td>Noida Sector ".($i+1)."</td>";
        $location="Noida Sector ".($i+1);
        for($j=1;$j<=8;$j++)
        {
            $time_id=$j;
            $sql="select status from final where location = '$location' AND date='".$cur_date."' AND time_id=$time_id";
            $query=mysqli_query($conn,$sql);
            if(!$query)
            {
                $status=0;
                echo "Hi";
            }
            else
            {
                if($row=mysqli_fetch_array($query))
                {   
                    //echo "hi";
                    $status=$row['status'];
                }
                else
                    $status=0;
                //echo $time_id;
            }
            if($i==0)
                echo "<td><input type='checkbox' id='yo' name=time[] value=".$j." value='".$status."' onclick='ck()' > $status </td>";
            else
                echo "<td><input type='checkbox' id='yo' name=time".$i."[] value=".$j."  value='".$status."' onclick='ck()' >$status</td>";
        }

        echo "</tr>";
    }
?>
5
  • what problem you are getting exactly? Commented Jun 25, 2015 at 12:55
  • I am not getting alert when I check boxes which do not have equal value.whereas I should get an alert Commented Jun 25, 2015 at 12:56
  • All of your checkboxes got the same id. Customize it somehow, like echo "<input type='checkbox' id='yo_".$i."' name='time[]' value='".$j."' onclick='ck()' /> $status</td>"; Commented Jun 25, 2015 at 12:56
  • For a checkbox type input, you need to make the id different. Please check with @AmBeam comment. Commented Jun 25, 2015 at 13:06
  • Ok if I provide different id then also how will I be able to bring the alert? Commented Jun 25, 2015 at 13:14

1 Answer 1

1

give unique id's to each checkbox even you are not using them.

Note: on your page id's should be unique.

try like this:

use your connection and other this ,it is for demo.

<?php 


    for($i = 0; $i < 150; $i++) {
        echo "<tr>";
        $cur_date=date("Y-m-d");
        echo "<td>Noida Sector ".($i+1)."</td>";
        $location="Noida Sector ".($i+1);
        for($j=1;$j<=8;$j++)
        {

            if($i==0)
                echo "<td><input type='checkbox' id='yo$i' name=time[] value=".$j." value='".$status."' onclick='ck(this.value)' > $status </td>";
            else
                echo "<td><input type='checkbox' id='yo$i' name=time".$i."[] value=".$j."  value='".$status."' onclick='ck(this.value)' >$status</td>";
        }

        echo "</tr>";
    }
?>

javascript:

<script>
    function ck(value)
    {
        var x,item,f;
       list= document.getElementsByTagName("input");
        f=value;
        console.log(f);
        for(i=1;i<list.length;++i)//change your logic here don't know what you are trying here
        {
            item=list[i];
            x=item.value;
            if(x!=f)
            {
                alert("Wrong type checkboxes selected!");
                break;
            }
        }
    }
</script>
Sign up to request clarification or add additional context in comments.

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.