PHP表单出现未定义的索引错误


PHP Undefined index error with form

我在提交表单时不断收到以下错误。我不确定它是什么。我在WAMP(windows)上运行该网站,并在dreamweaver CS6中编写。请注意,我是PHP的新手。谢谢

这是代码:

注意:未定义的索引:第29行S:''wamp''www''latech''contact.php中的消息

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php include 'includes/header.php'?>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<title>Anise Technologys</title>
</head>
<body>
<div class="wrapper">
<?php include 'includes/nav.php'?>
<div class="content">
<h1 id="title-center">Contact</h1>
		<?php
	
		// Check for Header Injections
		function has_header_injection($str) {
			return preg_match( "/['r'n]/", $str );
		}
		
		
		if (isset($_POST['contact_submit'])) {
			
			// Assign trimmed form data to variables
			// Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
			$name	= trim($_POST['name']);
			$email	= trim($_POST['email']);
			$tel = trim($_POST['tel']);
			$msg	= $_POST['message']; // no need to trim message
		
			// Check to see if $name or $email have header injections
			if (has_header_injection($name) || has_header_injection($email)) {
				
				die(); // If true, kill the script
				
			}
			
			if (!$name || !$email || !$msg) {
				echo '<h4 class="error">All fields required.</h4><a href="contact.php" class="button block">Go back and try again</a>';
				exit;
			}
			
			// Add the recipient email to a variable
			$to	= "brad@brightsidestudios.ca";
			
			// Create a subject
			$subject = "$name sent a message via your contact form";
			
			// Construct the message
			$message .= "Name: $name'r'n";
			$message .= "Email: $email'r'n'r'n";
			$message .= "Message:'r'n$msg";
			
			// If the subscribe checkbox was checked
			if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe' ) {
			
				// Add a new line to the $message
				$message .= "'r'n'r'nPlease add $email to the mailing list.'r'n";
				
			}
		
			$message = wordwrap($message, 72); // Keep the message neat n' tidy
		
			// Set the mail headers into a variable
			$headers = "MIME-Version: 1.0'r'n";
			$headers .= "Content-type: text/plain; charset=iso-8859-1'r'n";
			$headers .= "From: " . $name . " <" . $email . ">'r'n";
			$headers .= "X-Priority: 1'r'n";
			$headers .= "X-MSMail-Priority: High'r'n'r'n";
		
			
			// Send the email!
			mail($to, $subject, $message, $headers);
		?>
        		<!-- Show success message after email has sent -->
		<h5>Thanks for contacting Anise Technologys!</h5>
		<p>Please allow 24 hours for a response.</p>
		<p><a href="/final" class="button block">&laquo; Go to Home Page</a></p>
 		<?php
			} else {
		?>
<form method="post" action="" id="contact-form" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="name">Your Name</label>
<input type="text" id="name" name="name" placeholder="Name">
</div>
<div class="pure-control-group">
<label for="email">Your Emal</label>
<input type="email" id="email" name="email" placeholder="you@website.com">
</div>
<div class="pure-control-group">
<label for="tel">Your Telephone</label>
<input type="tel" id="tel" name="tel" placeholder="(xxx)xxx-xxxx">
</div>
<div class="pure-control-group">
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
</div>
<div class="pure-control-group">
<label for="subscribe">
<input type="checkbox" id="cb" name="subscribe" value="Subscribe">
Subscribe to Newsletter</label>
</div>
<div class="pure-control-group">
<input type="submit" class="pure-button pure-button-primary" name="contact_submit" value="Send Message">
</div>
</form>
		<?php
			}
		?>
</div>
</div>
</body>
</html><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php include 'includes/header.php'?>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<title>Anise Technologys</title>
</head>
<body>
<div class="wrapper">
<?php include 'includes/nav.php'?>
<div class="content">
<h1 id="title-center">Contact</h1>
		<?php
	
		// Check for Header Injections
		function has_header_injection($str) {
			return preg_match( "/['r'n]/", $str );
		}
		
		
		if (isset($_POST['contact_submit'])) {
			
			// Assign trimmed form data to variables
			// Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
			$name	= trim($_POST['name']);
			$email	= trim($_POST['email']);
			$tel = trim($_POST['tel']);
			$msg	= $_POST['message']; // no need to trim message
		
			// Check to see if $name or $email have header injections
			if (has_header_injection($name) || has_header_injection($email)) {
				
				die(); // If true, kill the script
				
			}
			
			if (!$name || !$email || !$msg) {
				echo '<h4 class="error">All fields required.</h4><a href="contact.php" class="button block">Go back and try again</a>';
				exit;
			}
			
			// Add the recipient email to a variable
			$to	= "brad@brightsidestudios.ca";
			
			// Create a subject
			$subject = "$name sent a message via your contact form";
			
			// Construct the message
			$message .= "Name: $name'r'n";
			$message .= "Email: $email'r'n'r'n";
			$message .= "Message:'r'n$msg";
			
			// If the subscribe checkbox was checked
			if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe' ) {
			
				// Add a new line to the $message
				$message .= "'r'n'r'nPlease add $email to the mailing list.'r'n";
				
			}
		
			$message = wordwrap($message, 72); // Keep the message neat n' tidy
		
			// Set the mail headers into a variable
			$headers = "MIME-Version: 1.0'r'n";
			$headers .= "Content-type: text/plain; charset=iso-8859-1'r'n";
			$headers .= "From: " . $name . " <" . $email . ">'r'n";
			$headers .= "X-Priority: 1'r'n";
			$headers .= "X-MSMail-Priority: High'r'n'r'n";
		
			
			// Send the email!
			mail($to, $subject, $message, $headers);
		?>
        		<!-- Show success message after email has sent -->
		<h5>Thanks for contacting Anise Technologys!</h5>
		<p>Please allow 24 hours for a response.</p>
		<p><a href="/final" class="button block">&laquo; Go to Home Page</a></p>
 		<?php
			} else {
		?>
<form method="post" action="" id="contact-form" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="name">Your Name</label>
<input type="text" id="name" name="name" placeholder="Name">
</div>
<div class="pure-control-group">
<label for="email">Your Emal</label>
<input type="email" id="email" name="email" placeholder="you@website.com">
</div>
<div class="pure-control-group">
<label for="tel">Your Telephone</label>
<input type="tel" id="tel" name="tel" placeholder="(xxx)xxx-xxxx">
</div>
<div class="pure-control-group">
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
</div>
<div class="pure-control-group">
<label for="subscribe">
<input type="checkbox" id="cb" name="subscribe" value="Subscribe">
Subscribe to Newsletter</label>
</div>
<div class="pure-control-group">
<input type="submit" class="pure-button pure-button-primary" name="contact_submit" value="Send Message">
</div>
</form>
		<?php
			}
		?>
</div>
</div>
</body>
</html>

您的name属性是msg,在PHP中称其为message

<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
$msg    = $_POST['message']; // no need to trim message

还有这里。。。

$message .= "Name: $name'r'n";
            $message .= "Email: $email'r'n'r'n";
            $message .= "Message:'r'n$msg";

第一次出现应该是$message =,因为在它还没有初始化时没有任何连接。

您正在使用

<textarea id="msg" name="msg" spellcheck="true" ></textarea>

而是使用

<textarea id="msg" name="message" spellcheck="true" ></textarea>