1

The checkbox is not working properly. It has 3 checkbox. when i select all three checkbox i don't get any error. but when i select 1 or 2 checkbox i am getting error like this :

Notice: Undefined index: chke in C:\wamp\www\test\secpage.php on line 16

<html>
<body>
<table>
    <form method="post" action="secpage.php">
        <tr>
            <td colspan="2"><center><h4>Registartion Form</h4></center></td>
        </tr>

        <tr>
            <td>Username : </td>
            <td><input type="text" name="txtname" value=""/></td>
        </tr>

        <tr>
            <td>Password : </td>
            <td><input type="password" name="txtpass" vale=""/></td>
        </tr>

        <tr>
            <td>Email : </td>
            <td><input typ="text" name="txtemail" value=""/></td>
        </tr>

        <tr>
            <td>Address : </td>
            <td><textarea name="add" /></textarea></td>
        </tr> 

        <tr>
            <td>Subjects : </td>
            <td><input type="checkbox" name="chkm" value="Maths"</td>Maths
            <td><input type="checkbox" name="chks" value="Science"</td>Science
            <td><input type="checkbox" name="chke" value="English"</td>English 
        </tr>

        <tr>
            <td>Gender : </td>
            <td><input type="radio" name="gen" value="Male"/>Male
                <input type="radio" name="gen" value="Female"/>Female
             </td>
         </tr>

         <tr>
            <td colspan="2"><center><input type="submit" value="Submit"/></center></td>
         </tr>



</body>
</html>

This is php page :

<html>
<body>
<?php
    $uname=$_POST["txtname"];
    $pass=$_POST["txtpass"];
    $email=$_POST["txtemail"];
    $add=$_POST["add"];
    $subm=$_POST["chkm"];
    $subs=$_POST["chks"];
    $sube=$_POST["chke"];   
    $gen=$_POST["gen"];
?>

<table>
<tr>
    <td>Username : </td>
    <td><?php echo $uname; ?></td>
</tr>

<tr>
    <td>Password : </td>
    <td><?php echo $pass; ?></td>
</tr>

<tr>
    <td>Email : </td>
    <td><?php echo $email; ?></td>
</tr>

<tr>
     <td>Address : </td>
     <td><?php echo $add; ?></td>
</tr>

<tr>
    <td>Subject Selected : </td>
    <td><?php echo $subm." ".$subs." ".$sube; ?></td>
</tr>

<tr>
    <td>Gender : </td>
    <td><?php echo $gen ?></td>
</tr>

</table>
</body>
</html>

Any help will be appreciated.

5 Answers5

1

You forget to close checkbox.

And if you post form without checking checkbox then it will give you error as you are getting now.

You can use @ to ignore this error but this is not a best practice.

Also

if(isset($_POST["chkm"]))
{
  $subm=$_POST["chkm"];
}

you can check if value is set or not using above conditions.

Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
0

A checkbox that isnt checked does not send it's value in the post. So $_POST["chke"] doesn't actually exist. You should check for existence before getting the value from the post.

if(isset($_POST["chke"]))
  $sube = $_POST["chke"];
Bazzz
  • 26,427
  • 12
  • 52
  • 69
0

Your input checkbox tag is incomplete. Therefore PHP can't read your post value.

Also, if it is not "checked", the POST value will not be sent.

Raptor
  • 53,206
  • 45
  • 230
  • 366
0

Something like the following will fix your undefined index error:

<?php
  $uname = array_key_exists("txtname", $_POST)  ? $_POST["txtname"]  : NULL;
  $pass  = array_key_exists("txtpass", $_POST)  ? $_POST["txtpass"]  : NULL;
  $email = array_key_exists("txtemail", $_POST) ? $_POST["txtemail"] : NULL;
  $add   = array_key_exists("add", $_POST)      ? $_POST["add"]      : NULL;
  $subm  = array_key_exists("subm", $_POST)     ? $_POST["subm"]     : NULL;
  $subs  = array_key_exists("subs", $_POST)     ? $_POST["subs"]     : NULL;
  $sube  = array_key_exists("sube", $_POST)     ? $_POST["sube"]     : NULL;
  $gen   = array_key_exists("gen", $_POST)      ? $_POST["get"]      : NULL;
?>

However it's normally nicer to create a function to handle this for you:

<?php

  function get_expected_post( $expected = array() ){
    $ret = (object) NULL;
    foreach ( $expected as $key ) {
      $ret->{$key} = array_key_exists($key, $_POST) ? $_POST[$key] : NULL;
    }
    return $ret;
  }

  $post = get_expected_post(array(
    'txtname', 
    'txtpass',
    'txtemail', 
    'add',
    'subm',
    'subs',
    'sube',
    'get'
  ))

?>

That way you can more easily expand the items in your form, and whenever you access the following you can be assured of it's existence — and so not trip an error:

<?php echo $post->subm; ?>

You could extend the above if you wanted the post names to be converted to different names in your script. Plus you could also add extra security like parsing the values for expected values and so forth.

Pebbl
  • 34,937
  • 6
  • 62
  • 64
0

In your html you're going to want to set the name the same for each checkbox

<td><input type="checkbox" name="chk[]" value="Maths"</td>Maths
<td><input type="checkbox" name="chk[]" value="Science"</td>Science
<td><input type="checkbox" name="chk[]" value="English"</td>English

Then, in your php file you can do this

$checkbox = $_POST['chk'];

$checkbox will now be an array of each checkbox that was selected.

Similar Issue here PHP and HTML Checkboxes using POST

Community
  • 1
  • 1