user8875600
user8875600

Reputation:

undeclared php variable in contact form

I'm having problems with a simple contact form.

The error I'm getting is:

Notice: Undefined index: terms in contact.php on line 29
Attention! You have to check the Privacy Policy box to accept our terms.

Code:

if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");

$name     = $_POST['name'];
$email    = $_POST['email'];
$comments = $_POST['comments'];
$terms    = $_POST['terms'];     // <---- line 29

if(trim($terms) == '') {
    echo '<div class="alert error"><div class="msg">Attention! You have to check 
        the Privacy Policy box to accept our terms.</div></div>';
    exit();
}

if(trim($comments) == '') {
    echo '<div class="alert error"><div class="msg">Attention! Please enter your 
        message.</div></div>';
    exit();
} 

if(get_magic_quotes_gpc()) {
    $comments = stripslashes($comments);
}

The template file is below:

<form action="<?php echo $this->config->get('config_url').'ajax/contact.php' ?>" method="post" id="contact_form" enctype="multipart/form-data">
    <table>
        <tr>
            <td>
                <label for="name"><?php echo $entry_name; ?></label>
                <input type="text" name="name" id="name" style="margin-right:20px" value="<?php echo $name; ?>" />
                <?php if ($error_name) { ?>
                <span class="error"><?php echo $error_name; ?></span>
                <?php } ?>
            </td>
            <td>
                <label for="email"><?php echo $entry_email; ?></label>
                <input type="text" name="email" id="email" value="<?php echo $email; ?>" />
                <?php if ($error_email) { ?>
                <span class="error"><?php echo $error_email; ?></span>
                <?php } ?>  
            </td>
        </tr>
    </table>

    <label for="enquiry"><?php echo $entry_enquiry; ?></label>
    <textarea name="enquiry" id="enquiry" cols="30" rows="5"><?php echo $enquiry; ?></textarea>
    <?php if ($error_enquiry) { ?>
    <span class="error"><?php echo $error_enquiry; ?></span>
    <?php } ?>

    <input type="checkbox" name="terms" value="<?php echo $terms; ?>" />
    <label for="terms">Tick this box to confirm you comply with our <a href="/privacy-policy/">Privacy Terms</a></label>

    <input type="submit" id="submit" value="<?php echo $button_continue; ?>" class="button dark-bt" />
</form>

Upvotes: 0

Views: 114

Answers (1)

Manojkumar B
Manojkumar B

Reputation: 216

Try this:

$terms    = isset($_POST['terms']) ? $_POST['terms'] : '';

You will get the checkbox field only if it is checked by the user in the form.

So, always check like this for checkbox and radio buttons.

Upvotes: 1

Related Questions