jeudi 25 juin 2015

javascript alert on checkbox value

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(i=1;i<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>";
}?>




Aucun commentaire:

Enregistrer un commentaire