php联系表单警报未显示

时间:2015-09-24 13:24:28

标签: php html

提交联系表后,它应显示“谢谢”,但不显示。当我们不输入输入类型时,错误不会显示。我的网站是alzirabarretto.com/webdesigner

<!-- PHP CONTACT FORM -->
    
    <?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $from = 'Portfolio Contact Form'; 
        $to = 'alzirabarretto@gmail.com'; 
        $subject = 'My Feedback ';
        
        $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 enter a valid email address';
        }
        
        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }
        
        // If there are no errors, send the email
        if (!$errName && !$errEmail && !$errMessage ) {
            if (mail ($to, $subject, $body, $from)) {
                $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
            } else {
                $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
            }
        }
    }
    ?>



<form class="form-horizontal" role="form" method="post" action="index.php">
                 
    <div class="row control-group">
        <div class="form-group col-xs-12 floating-label-form-group controls">
            <label>Name</label>
                <input type="text" class="form-control" placeholder="Name" id="name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
                <?php echo "<p class='text-danger'>$errName</p>";?>
        </div>
    </div>
                        
                
    <div class="row control-group">
         <div class="form-group col-xs-12 floating-label-form-group controls">
            <label>Email Address</label>
                <input type="email" class="form-control" placeholder="Email" id="email" value="<?php echo htmlspecialchars($_POST['email']); ?>">
             <?php echo "<p class='text-danger'>$errName</p>";?>
        </div>
    </div>
                        
    <div class="row control-group">
         <div class="form-group col-xs-12 floating-label-form-group controls">
            <label>Subject</label>
                <input type="subject" class="form-control" placeholder="Subject" id="sub">
             <?php echo "<p class='text-danger'>$errName</p>";?>
         </div>
    </div>
                        
    <div class="row control-group">
         <div class="form-group col-xs-12 floating-label-form-group controls">
            <label>Message</label>
                <textarea rows="5" class="form-control" placeholder="Message" id="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
             <?php echo "<p class='text-danger'>$errName</p>";?>
         </div>
    </div>
    <br>
    <div id="success"></div>
    <div class="row">
        <div class="form-group col-xs-12">
            <button type="submit" class="btn btn-success btn-lg" id="submit">Send</button>
            <div class="form-group">
                <div class="col-sm-10 col-sm-offset-2">
                <?php echo $result; ?>    
            </div>
        </div>
    </div>
</form>

1 个答案:

答案 0 :(得分:0)

$结果在打印时超出范围。尝试在开始时定义它。

    $result="";

将以下代码添加到要打印的位置。

      if($result!="")
      {
         echo $result;
      }