PHP泄漏&未定义的变量

时间:2017-02-26 19:01:04

标签: php html css forms contact

第一次发帖。我对此很新。我有使用HTML / CSS的经验,但使用JS或PHP的经验不多。我正在尝试为我的网站创建一个联系表单,用户将在字段未完成时收到警报,然后在一切正确的情况下将其信息发送到我的电子邮件中。

我使用了一个教程来获取大部分代码,但我根据自己的需要定制了部分代码,并添加了一些重复的内容。

Screenshot

然而,它不起作用。或者我认为它被称为PHP泄漏?我收到错误提到未定义的变量。无论如何,这是PHP代码,后面是HTML。

<?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $tel = $_POST['tel'];
    $company = $_POST['companyl'];
    $message = $_POST['message'];
    $from = 'name'; 
    $to = 'johndoe@domain.com'; 
    $subject = 'company';

    $body ="From: $name\n E-Mail: $email\n Message:\n $message";
    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please provide us with an email address.';
    }

    //Check if phone number  has been entered
    if (!$_POST['tel']) {
        $errTel = 'Please enter a number we can reach you at.';
    }

    //Check if Company name has been entered
    if (!$_POST['company']) {
        $errCompany = 'What is the name of your company?';
    }

    //Check if message has been entered
    if (!$_POST['message']) {
        $errMessage = 'Please tell us more about your company and your needs.';
    }

    //Check if simple anti-bot test is correct
    if ($human !== 4) {
        $errHuman = 'Incorrect answer! Try again.';
    }

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errTel && !$errCompany && !$errHuman) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">We will get in touch with you shortly!</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
    }
}
    }
?>

HTML:

<form role="form" method="post" action="index.php">
    <div class="row">
        <div class="col-lg-6 form-group">
            <input type="text" class="formbox text-muted" id="name" name="name" placeholder="Name" required value="<?php echo htmlspecialchars($_POST['name']); ?>">
                <?php echo "<p class='text-danger'>$errName</p>";?>
        </div>
        <div class="col-lg-6 form-group">
            <input type="tel" class="formbox text-muted" id="tel" name="tel" placeholder="Phone number" required value="<?php echo htmlspecialchars($_POST['tel']); ?>">
                <?php echo "<p class='text-danger'>$errTel</p>";?>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-6 form-group">
            <input type="email" class="formbox text-muted" id="email" name="email" placeholder="Email address" required value="<?php echo htmlspecialchars($_POST['email']); ?>">
            <?php echo "<p class='text-danger'>$errEmail</p>";?>

        </div>
        <div class="col-lg-6 form-group">
            <input type="text" class="formbox text-muted" id="company" name="company" placeholder="Name of your company" required value="<?php echo htmlspecialchars($_POST['company']); ?>">
            <?php echo "<p class='text-danger'>$errCompany</p>";?>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-12 form-group">
                <textarea name="message" class="messagebox text-muted" id="message" name="message" placeholder="Tell us about the company you're trying to pitch" required></textarea>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-6 pull-left">
            <input type="text" class="formbox text-muted" id="human" placeholder="What is is 2+2?" required value="<?php echo htmlspecialchars($_POST['human']); ?>">
            <?php echo "<p class='text-danger'>$errHuman</p>";?>
        </div>
        <div class="col-lg-4 pull-right">
            <button id=" submit" type="submit" class="inputsubmit btn btn-block btn-default btn-xl sr-button" value="send">Send Message!</button>
        </div>
        <div class="col-lg-6 pull-right">
            <?php echo $result; ?>
        </div>
    </div>
</form>

2 个答案:

答案 0 :(得分:0)

一个简单的修复:如果未设置(不存在),您可以将所有$_POST['x']变量设置为空字符串,这将消除未定义的变量错误。

foreach( array( 'name', 'tel', 'email', 'company', 'human' ) as $key ){
    if ( !isset( $_POST[ $key ] ) ) $_POST[ $key ] = '';
}

这应该在PHP脚本的顶部完成。

您正在尝试在HTML($_POST['name'])中使用之前输入的值(value="<?php ... ?>")。如果用户第一次访问该表单,他们没有提供任何名称,也没有发布任何表单数据,因此$_POST['name']不存在。

更好的解决方法:如果该值不存在,请不要输出该值。变化:

<input type="text" class="formbox text-muted" id="name" name="name" placeholder="Name" required value="<?php echo htmlspecialchars($_POST['name']); ?>">

为:

<input type="text" class="formbox text-muted" id="name" name="name" placeholder="Name" required value="<?php if( isset( $_POST['name'] ) ) { echo htmlspecialchars($_POST['name']); } ?>">

此外,您似乎在早期使用companyl而不是company时输入错字:

$company = $_POST['companyl'];

<强>更新 您在评论中回答说,您仍然会收到有关错误消息的未定义变量。你应该在这里进行一些类似的检查,以确保向echo发送错误消息:

<?php echo "<p class='text-danger'>$errTel</p>";?>

变为:

<?php if( isset( $errTel ) ) { echo "<p class='text-danger'>$errTel</p>"; } ?>

(依此类推,适用于所有事件)

答案 1 :(得分:0)

您应该在输入值字段中使用小的内联php条件 -

<?php echo ($_POST['anything']) ? $_POST['anything'] : ''?>

说明是当您尝试正常加载页面时,页面中缺少$_POST。因此,只需检查变量是否存在就行了。如果存在则使用变量,否则留空值。