表单,在同一个php文件上接收变量

时间:2013-09-02 00:35:31

标签: php forms

我想要做的是创建一个简单的联系表单。我有一个名为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代码放在同一个文件中。那可能吗?我该怎么办?

谢谢。

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

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

if(count($_POST)) > 0)

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

答案 2 :(得分:0)

您可能希望使用PHP的isset功能。

$name = $_POST['name'];
if(isset($name) && $name != ''){
blah....
}
else {
blah...
}