我试图将我的表单输入框收集的信息发送到电子邮件中。电子邮件正在发送,但电子邮件消息不显示输入框中输入的信息。
(这就是我得到的)
姓名:
号码:
地点:
电子邮件:
我是PHP新手,非常感谢任何建议!
PHP
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone= $_POST['phone'];
$location= $_POST['location'];
$formcontent = "
Name: $name \n
Number: $phone \n
Location: $location \n
Email: $email";
$recipient = "chris@knorthstudios.com";
$subject = "Boston Calling Street Team Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank you";
?>
表格代码:
<form method="POST" action="contact.php">
label>Name:</label>
<input type="text" class="inputbox" name="name" required>
<label>Age:</label>
<input class="inputbox" name="age" required>
<label>Location (City/State): </label>
<input class="inputbox" name="location" required>
<label>Phone number: </label>
<input class="inputbox" name="phone" required>
<label>E-mail address:</label>
<input class="inputbox" name="email" required>
<label>What is your favorite flavor of icecream</label>
<textarea class="inputbox textinputbox" name="icecream" required></textarea>
<input class="streetteamsubmit" type="submit" name="submit" value="SUBMIT FORM">
</form>
答案 0 :(得分:0)
a)启用错误报告。在代码顶部添加:
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
b)php代码和表单是否在同一个文件中?
然后你应该检查页面是否已经通过POST方法发送。访问者第一次到达联系表格时将始终使用GET方法。 (他只是到达页面,仍然需要填写表格区域)
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
// visitor has sent the form
}
c)你永远不知道$ _POST元素是否真的存在,因此你应该用isset()函数检查它
// initializing with a default value
$name = '';
// overwrite value if $_POST element exists
if(isset($_POST['name']))
{
$name = $_POST['name'];
}
d)在这里,您将看到一个带有验证的良好工作形式的基本示例。
<?php
// Function that validates your formfields and generate errors
function validate($naam, $email)
{
$errors = array();
// validation for name
if(strlen($naam) < 2)
$errors[] = 'U heeft geen naam ingevuld.';
// validation for email
if(!strlen($email))
$errors[] = 'U heeft geen email adres ingevuld.';
else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
$errors[] = 'U heeft een ongeldig email adres ingevuld.';
// return array with errors
return $errors;
}
// initialization of variables with default values
$naam = '';
$email = '';
$errors = array();
// if the form has been submited
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
// overwrite the variables with the $_POST elements
$naam = $_POST['naam'];
$email = $_POST['email'];
// validate the variables
$errors = validate($naam, $email);
// If there aren't any errors redirect to another page
if(!count($errors))
{
// Here comes the place to send an email!!!
header('Location: thankyou.html');
exit;
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>a titel</title>
</head>
<body>
<ul id="errors">
<?php
foreach($errors as $error)
echo '<li>' . $error . '</li>';
?>
</ul>
<form action="" method="post">
<input type="text" name="naam" value="<?php echo $naam; ?>" />
<input type="email" name="email" value="<?php echo $email; ?>" />
<button type="submit">Verzenden</button>
</form>
</body>
</html>