我想做的是创建一个简单的联系人表单
<form action="process-contact.php" method="post">
<input type="text" name="name" placeholder="Nombre*">
<input type="text" name="company" placeholder="Compañía">
<input type="text" name="position" placeholder="Posición">
<input type="text" name="country" placeholder="País*">
<input type="text" name="email" placeholder="Correo electrónico*">
<input type="text" name="subject" placeholder="Asunto">
<textarea name="message" placeholder="Mensaje*"></textarea>
<input type="submit" value="Enviar">
</form>
我有这个php代码:
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$headers = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
$to = "email@domain.com";
if($name != '' && $country != '' && $email != '' && $message != ''){
mail($to, $subject, $message, $headers); //calling php mail function
echo 'Thank''s for contacting us. We will be answering back soon.<br><br><a href="index.php">Go back.</a>';
}else{
echo 'Plese verify all the fields and try to send the form again.<br><br><a href="index.php">Go back.</a>';
}
php代码在另一个文件中,但我希望php代码在同一个文件文件中。这可能吗?我该怎么做?
谢谢。
您可以将所有内容放在一个文件中,并使用action=""
你的标题中有错误,通过使用这些,电子邮件最终出现在我的SPAM框中,所以我用正确的标题更改了这些标题,并使用$message
变量添加了其他详细信息。
我添加了header('Location: thank_you.php');
,如果所有字段都已填充,我将重定向到该页面。创建一个名为thank_you.php
的文件,或者将其更改为转到您的主页。
我替换了:
if($name != '' && $country != '' && $email != '' && $message != ''){
通过:
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['country']) ||
empty($_POST['message'])) {
echo "Fill in all the fields. All marked by an asterisk are mandatory.";
}
表单和PHP处理程序(测试成功)
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$to = "email@example.com";
$message = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['country']) ||
empty($_POST['message'])) {
echo "Fill in all the fields. All marked by an asterisk are mandatory.";
}
else {
$headers = "MIME-Version: 1.0'n";
$headers .= "Content-Type: text/html; charset=iso-8859-1'n";
$headers .= "From: $email" . "'r'n" .
"Reply-To: $email" . "'r'n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers); //calling php mail function
header("Location: thank_you.php");
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="" method="post">
<input type="text" name="name" placeholder="Nombre*">
<input type="text" name="company" placeholder="Compañía">
<input type="text" name="position" placeholder="Posición">
<input type="text" name="country" placeholder="País*">
<input type="text" name="email" placeholder="Correo electrónico*">
<input type="text" name="subject" placeholder="Asunto">
<textarea name="message" placeholder="Mensaje*"></textarea>
<input type="submit" name="submit" value="Enviar">
</form>
</body>
</html>
或者您可以将其用作PHP处理程序
<?php
$name = $_POST['name'];
$company = $_POST['company'];
$position = $_POST['position'];
$country = $_POST['country'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$to = "email@example.com";
$message = "Sent by: ".$name.' ('.$email.');'." Country: ".$country.'; Company: '.$company. '; Position: '.$position.';';
if(!empty($_POST['name']) &&
!empty($_POST['email']) &&
!empty($_POST['country']) &&
!empty($_POST['message'])) {
$headers = "MIME-Version: 1.0'n";
$headers .= "Content-Type: text/html; charset=iso-8859-1'n";
$headers .= "From: $email" . "'r'n" .
"Reply-To: $email" . "'r'n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers); //calling php mail function
echo 'Thanks for contacting us. We will be answering back soon.<br><br><a href="index.php">Go back.</a>';
// to prevent re-submission, use header but NOT with echo.
// header("Location: thank_you.php");
}else{
echo 'Please verify all the fields.<br><br><a href="index.php">Go back.</a>';
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="" method="post">
<input type="text" name="name" placeholder="Nombre*">
<input type="text" name="company" placeholder="Compañía">
<input type="text" name="position" placeholder="Posición">
<input type="text" name="country" placeholder="País*">
<input type="text" name="email" placeholder="Correo electrónico*">
<input type="text" name="subject" placeholder="Asunto">
<textarea name="message" placeholder="Mensaje*"></textarea>
<input type="submit" name="submit" value="Enviar">
</form>
</body>
</html>
将表单的action
属性设置为包含表单和php代码的文件名,然后将所有表单处理php代码包含在以下条件中。
if(count($_POST)) > 0)
这将确保php代码仅在提交表单时运行。
您可能想要使用PHP的isset功能。
$name = $_POST['name'];
if(isset($name) && $name != ''){
blah....
}
else {
blah...
}