联系表单中未声明的php变量

时间:2018-05-30 09:59:18

标签: php variables contact

我在使用简单的联系表单时遇到了问题。

我得到的错误是:

"注意:未定义的索引:第29行的contact.php中的术语 注意!您必须查看隐私政策框以接受我们的条款。"

第29行

$ terms = $ _POST [' terms'];

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

$name     = $_POST['name'];
$email    = $_POST['email'];
$comments = $_POST['comments'];
$terms    = $_POST['terms'];

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);
}

模板文件如下:

<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>

1 个答案:

答案 0 :(得分:1)

试试这个:

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

只有当用户在表单中检查时,才会显示复选框字段。

因此,请务必按照复选框单选按钮进行检查。