0

我想做的是创建一个简单的联系表格。我有一个名为 contact.php 的文件,其简单的 html 表单:

<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 代码放在同一个文件中。那可能吗?我该怎么做?

谢谢你。

4

3 回答 3

1

您可以将所有内容放在一个文件中并使用action=""

您的标头中有错误,通过使用这些,电子邮件最终出现在我的垃圾邮件框中,因此我使用正确的标头更改了这些标头,并使用$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>
于 2013-09-02T01:28:59.093 回答
0

将表单的action属性设置为包含表单和php代码的文件的名称,然后在以下条件中包含所有表单处理php代码。

if(count($_POST)) > 0)

这将确保 php 代码仅在提交表单时运行。

于 2013-09-02T00:41:00.253 回答
0

也许你会想要使用 PHP 的 isset 特性。

$name = $_POST['name'];
if(isset($name) && $name != ''){
blah....
}
else {
blah...
}
于 2013-09-02T00:43:10.470 回答