Hi

I am selecting wdays1 checkbox is checked and remaining checkbox is unchecked after submitting, i am getting an error message as below can any one please help me to resolve the issue

Error

1 Notice: Undefined index: wdays2 in C:\xampp\htdocs\smart\checkvalue.php on line 10 0 Notice: Undefined index: wdays3 in C:\xampp\htdocs\smart\checkvalue.php on line 14 0 Notice: Undefined index: wdays4 in C:\xampp\htdocs\smart\checkvalue.php on line 18 0 Notice: Undefined index: wdays5 in C:\xampp\htdocs\smart\checkvalue.php on line 22 0 Notice: Undefined index: wdays6 in C:\xampp\htdocs\smart\checkvalue.php on line 26 0 Notice: Undefined index: wdays7 in C:\xampp\htdocs\smart\checkvalue.php on line 30 0

Code:

<?php
if(isset($_POST['Submit']))
{
    if($_POST['wdays1']=='')

        echo $wdays1v = 0;
    else
        echo $wdays1v = 1;

    if($_POST['wdays2']=='')
        echo $wdays2v = 0;
    else
        echo $wdays2v = 1;
    if($_POST['wdays3']=='')
        echo $wdays3v = 0;
    else
        echo $wdays3v = 1;
    if($_POST['wdays4']=='')
        echo $wdays4v = 0;
    else
        echo $wdays4v = 1;
    if($_POST['wdays5']=='')
        echo $wdays5v = 0;
    else
        echo $wdays5v = 1;
    if($_POST['wdays6']=='')
        echo $wdays6v = 0;
    else
        echo $wdays6v = 1;
    if($_POST['wdays7']=='')
        echo $wdays7v = 0;
    else
        echo $wdays7v = 1; 


}
<form action="checkvalue.php" method="post" enctype="multipart/form-data">
  <div class="col-sm-2">
    <input class="icheck" type="checkbox" id="wdays1" name="wdays1" value='1'>
    Monday </div>
  <div class="col-sm-2">
    <input class="icheck" type="checkbox" id="wdays2" name="wdays2" value='1'>
    Tuesday </div>
  <div class="col-sm-2">
    <input class="icheck" type="checkbox" id="wdays3" name="wdays3" value='1'>
    Wednesday </div>
  <div class="col-sm-2">
    <input class="icheck" type="checkbox" id="wdays4" name="wdays4" value='1'>
    Thursday </div>
  <div class="col-sm-2">
    <input class="icheck" type="checkbox" id="wdays5" name="wdays5" value='1'>
    Friday </div>
  <div class="col-sm-2">
    <input class="icheck"  type="checkbox" id="wdays6" name="wdays6" value='1'>
    Saturday </div>
  <div class="col-sm-2">
    <input class="icheck" type="checkbox" id="wdays7" name="wdays7" value='1'>
    Sunday </div>
    <input name="Submit" type="submit" value="Submit">
</form>

if the checkboxes are not checked they wont appear in the $_POST superglobal.
Try to print your post data like print_r($_POST) and you would know the reason.

Thanks Network18

If i checked wdays2,wdays4,wdays7. Then i am getting a below message

Notice: Undefined index: wdays1 in C:\xampp\htdocs\smart\checkvalue.php on line 120
1
Notice: Undefined index: wdays3 in C:\xampp\htdocs\smart\checkvalue.php on line 124
1
Notice: Undefined index: wdays5 in C:\xampp\htdocs\smart\checkvalue.php on line 128

Notice: Undefined index: wdays6 in C:\xampp\htdocs\smart\checkvalue.php on line 130
1

Hi

i have solved my issue with help of the below code

<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
        $(function(){
            $( "input[type=checkbox]" ).click(function(){
                if($( "#Monday" ).is( ':checked' )){
                    $( '.msg' ).text( '1' );
                }else{          
                    $( '.msg' ).text( '0' );
                }
                if($( "#Tuesday" ).is( ':checked' )){
                    $( '.msg1' ).text( '1' );
                }else{          
                    $( '.msg1' ).text( '0' );
                }
                if($( "#Wednesday" ).is( ':checked' )){
                    $( '.msg2' ).text( '1' );
                }else{          
                    $( '.msg2' ).text( '0' );
                }
                if($( "#Thursday" ).is( ':checked' )){
                    $( '.msg3' ).text( '1' );
                }else{          
                    $( '.msg3' ).text( '0' );
                }
                if($( "#Friday" ).is( ':checked' )){
                    $( '.msg4' ).text( '1' );
                }else{          
                    $( '.msg4' ).text( '0' );
                }
                if($( "#Saturday" ).is( ':checked' )){
                    $( '.msg5' ).text( '1' );
                }else{          
                    $( '.msg5' ).text( '0' );
                }
                if($( "#Sunday" ).is( ':checked' )){
                    $( '.msg6' ).text( '1' );
                }else{          
                    $( '.msg6' ).text( '0' );
                }
            })
        });
    </script>
</head>
<body>
<form>
<input type="checkbox" name="Monday" id="Monday" value="Monday" />
Monday &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="Tuesday" id="Tuesday" value="Tuesday" />
Tuesday &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="Wednesday" id="Wednesday" value="Wednesday" />
Wednesday </br>
<input type="checkbox" name="Thursday" id="Thursday" value="Thursday" />
Thursday &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="Friday" id="Friday" value="Friday" />
Friday &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="Saturday" id="Saturday" value="Saturday" />
Saturday &nbsp;&nbsp;&nbsp;
<input type="checkbox" name="Sunday" id="Sunday" value="Sunday" />
Sunday &nbsp;&nbsp;&nbsp;<br>
</form>
</body>
</html>

A suggestion: this does not solve the problem if the client disables javascript. The suggestion of network18 is correct, in your server side you should check if the index key exists. So instead of:

if($_POST['wdays1']=='')
    echo $wdays1v = 0;
else
    echo $wdays1v = 1;

It should be:

# default
$wdays1v = 0;

# if exists
if(array_key_exists('wdays1', $_POST))
{
    # and if it is not empty
    if( ! empty($_POST['wdays1'])
    {
        $wdays1v = 1;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.