Php 变量存在,但给出未定义的变量错误

Php 变量存在,但给出未定义的变量错误,php,mysqli,Php,Mysqli,这真的让我抓狂……我一直在得到一个未定义的变量,即使它在那里! 我如何消除这个错误?更不用说,由于使用xampp,而不是在实时服务器上,我甚至不确定表单是否可以工作 contact.php上的表单 <div class="col-md-6 col-md-offset-3 emailForm"> <?php if (isset($result)) { echo "$result";} ?> &l

这真的让我抓狂……我一直在得到一个未定义的变量,即使它在那里! 我如何消除这个错误?更不用说,由于使用xampp,而不是在实时服务器上,我甚至不确定表单是否可以工作

contact.php上的表单

        <div class="col-md-6 col-md-offset-3 emailForm">
                    <?php if (isset($result)) { echo "$result";} ?>
            <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                <div class="form-group">
                    <label for="name">Your Name:</label>
                    <input type="text" name="name" class="form-control" value="<?php echo isset($_POST['name']) ? $_POST['name'] : ''; ?>" />

                </div>
                <div class="form-group">
                    <label for="subject">Subject:</label>
                    <input type="text" name="subject" class="form-control" value="<?php echo isset($_POST['name']) ? $_POST['subject'] : ''; ?>" />
                </div>
                <div class="form-group">
                    <label for="email">Your Email Address:</label>
                    <input type="email" name="email" class="form-control" value="<?php echo isset($_POST['name']) ? $_POST['email'] : ''; ?>" />
                </div>
                <div class="form-group">
                    <label for="message">Your Message:</label>
                    <textarea class="form-control" rows="5" name="message"><?php echo isset($_POST['name']) ? $_POST['message'] : ''; ?></textarea>
                </div>
                <div class="text-center">
                    <input type="submit" name="submit" class="btn btn-success btn-lg" value="Submit"/>
                </div>
            </form>
    <!-- Contact Form Ends -->
        </div>
sendmail.php代码

<?php

     if(isset($_POST["submit"])) {              
         if (!$_POST['name']) { 
             $error="<br />Please enter your name"; 
         }       
         if (!$_POST['subject']) { 
             $error.="<br />Please enter a subject"; 
         }          
         if (!$_POST['email']) { 
             $error.="<br />Please enter your email address"; 
         }          
         if (!$_POST['message']) { 
             $error.="<br />Please enter a message"; 
         }                   
         if ($_POST['email']!="" AND !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {          
            $error.="<br />Please enter a valid email address"; 
         }          
         if ($error) { 
             $result='<div class="alert alert-danger"><strong>There were error(s) in your form:</strong>'.$error.'</div>';           
         } else { 
            if (mail("support@kf.com", "New Online Message!", "Name: ". $_POST['name']." 
                Name: ".$_POST['name']."             
                Subject: ".$_POST['subject'])) { 
         $result='<div class="alert alert-success text-center"><strong>Thank you!</strong> Please allow 24 to 48 hours for our support team to get back to you. Thank you. </div>';      
         } else {        
            $result='<div class="alert alert-danger">Sorry, there was an error sending your message. Please try again later.</div>';         
        }    
    }    
}

?>
请尝试使用以下代码:


您在if块中定义了$error变量,它的作用域仅在if块中。请在开始时在if块之外定义它

$error仅在以下情况下设置$_POST['name']使用=。否则,可以使用..=,将其附加到$error。您必须首先设置$error=;。这在某些服务器上起作用的原因是您禁用了PHP通知。还请注意,您正在向电子邮件中注入标题,允许任何人使用您的联系人表单发送任何其他电子邮件,甚至发送上千封电子邮件。因此,在if语句之前出现$error??我不明白领导者是谁。我甚至不知道如何在没有邮件头的情况下保护这种类型的电子邮件,我敢肯定,要求这里的任何人打字都太过分了。无论如何,谢谢你的提醒。@Arif_suhail_123,我现在知道了,只是好奇标题部分关于可能的垃圾邮件