Hi,

I've got a form which captures data into a MySQL database. Trouble is my radio buttons and checkbox both cause undefined index error messages and I don't know how to stop them from happening. Futhermore, for some reason I need to define things twice otherwise it causes undefined index error messages as well, hence why you see $NAME = cleanInput($_POST['NAME'], $conn); for example near the top of the code and then further down under the // Sanitise details comment.

Really unsure how to fix these, so any help would be appreciated. For my checbox I dont need it to write anything to my database, just needs to see if its checked then allow the form to be submitted, otherwise show the error message.

The radio buttons and checkbox are the last two rows in my form table called "OVER18" and "TERMS".

PHP Code:
<?php
require_once('db.php');
require_once(
'functions.php');
// date
$DATE date(cleanInput("Y-m-d"$conn));

$errors = array();

// If request is a form submission
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    
$NAME cleanInput($_POST['NAME'], $conn);
    
$EMAIL cleanInput($_POST['EMAIL'], $conn);
    
$COMMENTS cleanInput($_POST['COMMENTS'], $conn);
    
$OVER18 cleanInput($_POST['OVER18'], $conn);
    
$TERMS cleanInput($_POST['TERMS'], $conn);


// Validation
// Check NAME is not less than 2 characters
    
if (strlen($NAME) < 2) {
        
$errors['NAME'] = "Your name is not long enough";
    }

    
// Check TELEPHONE is valid
    
if (=== preg_match("/^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/"$_POST['TELEPHONE'])) {
        
$errors['TELEPHONE'] = "Please enter valid phone number";
    }

    
// Check EMAIL is not less than 2 characters
    
if (strlen($EMAIL) < 2) {
        
$errors['EMAIL'] = "Your email address is not long enough";
    }

    
// Check COMMENTS is not less than 3 characters
    
if (strlen($COMMENTS) < 3) {
        
$errors['COMMENTS'] = "Please enter a comment";
    }

    
// Check OVER 18
    
if( !isset($_POST['radio']) || ($_POST['radio'] != 'yes' && $_POST['radio'] != 'no') ) {
   
$errors['radio'] = 'Please answer this question';
    } 

    
// Check TERMS have been agreed
    
if ($TERMS == "No") {
        
$errors['TERMS'] = "It is required of you to agree to the terms before continuing";
    }

    
// If no validation errors
    
if (=== count($errors)) {

        
// Sanitise details
        
$NAME cleanInput($_POST['NAME'], $conn);
        
$TELEPHONE cleanInput($_POST['TELEPHONE'], $conn);
        
$EMAIL cleanInput(trim($_POST['EMAIL']), $conn);
        
$COMMENTS cleanInput($_POST['COMMENTS'], $conn);
        
$OVER18 cleanInput($_POST['OVER18'], $conn);
        
$TERMS cleanInput($_POST['TERMS'], $conn);

        
// Insert user into the database
        
$query "
    INSERT INTO
        testform  (
            DATE
          , NAME
          , TELEPHONE
          , EMAIL
          , COMMENTS
          , OVER18
          , TERMS
    ) VALUES (
            '
$DATE'
          , '
$NAME'
          , '
$TELEPHONE'
          , '
$EMAIL'
          , '
$COMMENTS'
          , '
$OVER18'
          , '
$TERMS'
          )"
;


        
// for debugging
        
print_r($_POST);

        
$result mysqli_query($conn$query) or die(mysqli_error($conn) . $query);

        if (
$result != FALSE) {
            
// Form submitted successfully
            
header("Location: thankyou.php");
            exit;
        }
    }
} else {

    
//  DEBUGGING ONLY - DISABLE IN PRODUCTION SITE
    // echo "<br/><br /> MySQLi Error: " . mysqli_error($conn);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Untitled Document</title>
        <link rel="stylesheet" type="text/css" href="styles.css" />
    </head>
    <body>
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">       
            <table class="form">
                <tr class="<?php echo form_row_class("NAME"$errors); ?>">
                    <th><label for="NAME">Telephone</label></th>
                    <td><input name="NAME" id="NAME" type="text" value="<?php echo isset($_POST['NAME']) ? hsc($_POST['NAME']) : ''?>" />
                        <?php echo error_for("NAME"$errors); ?></td>
                </tr>
                <tr class="<?php echo form_row_class("TELEPHONE"$errors); ?>">
                    <th><label for="TELEPHONE">Telephone</label></th>
                    <td><input name="TELEPHONE" id="TELEPHONE" type="text" value="<?php echo isset($_POST['TELEPHONE']) ? hsc($_POST['TELEPHONE']) : ''?>" />
                        <?php echo error_for("TELEPHONE"$errors); ?></td>
                </tr>
                <tr class="<?php echo form_row_class("EMAIL"$errors); ?>">
                    <th><label for="EMAIL">Email Address</label></th>
                    <td><input name="EMAIL" id="EMAIL" type="text" value="<?php echo isset($_POST['EMAIL']) ? hsc($_POST['EMAIL']) : ''?>" />
                        <?php echo error_for("EMAIL"$errors); ?></td>
                </tr>
                <tr class="<?php echo form_row_class("COMMENTS"$errors); ?>">
                    <th><label for="COMMENTS">Comments</label></th>
                    <td><textarea name="COMMENTS" id="COMMENTS"><?php echo isset($_POST['COMMENTS']) ? hsc($_POST['COMMENTS']) : ''?></textarea>
                        <?php echo error_for("COMMENTS"$errors); ?></td>
                </tr>

                <tr class="<?php echo form_row_class("OVER18"$errors); ?>">
                    <th><label for="OVER18">Tick box to agree to terms and conditions</label></th>
                    <td colspan="2">
                    
                    <label for="OVER18_YES">Yes</label>
                    <input type="radio" name="OVER18" id="OVER18_YES value="yes" <?php echo isset($_POST['OVER18']) && $_POST['OVER18'] == 'yes' 'checked="checked"' ''?>/>
                    <label for="OVER18_NO">NO</label>
                    <input type="radio" name="OVER18" id="OVER18_NO" value="no" <?php echo isset($_POST['OVER18']) && $_POST['OVER18'] == 'no' 'checked="checked"' ''?>/>
        
                        <?php echo error_for("OVER18"$errors); ?></td>
                </tr>

                <tr class="<?php echo form_row_class("TERMS"$errors); ?>">
                    <th><label for="TERMS">Tick box to agree to terms and conditions</label></th>
                    <td colspan="2">
                        <input type="checkbox" name="TERMS" id="TERMS" value="Agreed" <?php echo isset($_POST['TERMS']) && $_POST['TERMS'] == 'Agreed' 'checked="checked"' ''?>/> />
                        <?php echo error_for("TERMS"$errors); ?></td>
                </tr>
                <tr>
                    <th></th>
                    <td>
                        <input type="submit" value="Go!" /></td>
                </tr>
            </table>
        </form>
    </body>
</html>