1

我被困住了,我希望有人能帮助我。我正在尝试使用 PHP action="" method="post" 向我的网站添加一个简单的联系表单。当我单击“提交”按钮时,不是运行脚本,而是打开“send_contact.php”页面,您会看到所有代码。我已经检查并测试了我的服务器(blue.host),它被设置为运行 php 脚本。我已经尝试了一切,我很茫然。这是HTML:

<div class="form">
<form id="myform" name="myform" action="send_contact.php" method="post">
<label>Name</label>

<input type="text" name="name" id="name" />                 
<label>E-mail</label>
<input type="text" name="email" id="email" />

<label>Phone</label>
<input type="text" name="phone" id="phone" />

<label>Message</label>
<textarea name="message" rows="5" cols="60"></textarea>

<button name="send" type="submit">send</button>
<button name="reset" type="reset">reset</button>
<div class="spacer"></div>
</form><!--end myform div-->
</div><!--end form div-->                       

这是 PHP 脚本:

<?php

// From
$header="from: $name <$mail_from>";

// Mail of sender
$mail_from="$customer_mail";

// Contact phone
$phone ="$phone"; 

// Details
$message="$message";


// Enter your email address
$to ='jacine.arias@gmail.com';

$send_contact=mail($to,$header,$mail,$phone,$message);


// Check, if message sent to your email
// display message "Thank you! Your message has been recieved."
if($send_contact){
echo "Thank you! Your message has been recieved.";
}
else {
echo "ERROR";
}
?>

这是CSS:

/*-----------FORM---------------*/

.form {
    border: 1px solid #262223;
    margin: 0 auto;
    padding: 14px;
    width: 375px;

}

.form label {
    display: block;
    text-align: right;
    width: 80px;
    float: left;
}

.form input {
    float: left;
    font-size: 12px;
    padding: 4px 2px;
    border: 1px solid #262223;
    width: 270px;
    margin: 2px 0 10px 10px;
}

textarea {
    float: left;
    font-size: 12px;
    padding: 4px 2px;
    border: 1px solid #262223;
    width: 270px;
    margin: 2px 0 20px 10px;
}

button[type="submit"] {
    clear: both;
    color: #fff;
    width: 100px;
    height: 31px;
    text-align: center;
    background: #F20F4B;
    margin-right: 5px;
    float: left;
}

button[type="reset"] {
    clear: both;
    color: #fff;
    width: 100px;
    height: 31px;
    text-align: center;
    background: #F20F4B;
    display: inline;
}

这是我网站上的实际页面:http: //jacineariasdesign.jacineariasweb.com/contact.html 任何帮助完成这项工作将不胜感激!谢谢!

4

3 回答 3

1

来,试试这个。经过测试,可以使用额外的安全功能进行修改,但它适用于您提供的表单。

还添加了一些错误检查。

如果它对您不起作用,那么您的“您的”服务器上存在 PHP 问题。

你原来的 PHP 脚本从来没有一个Subject条目开始。

<?php

$headersep = "\r\n";
$header = "From: $name <$email>" . $headersep . "Reply-To: $name <$email> . $headersep";
$email = $_POST['email'];
$subject="Your subject here";
$phone = $_POST['phone']; 
$message = "From: $name\n\nMessage: $message\n\nEmail: $email\n\nTelephone: $phone";
$to ='youremail@example.com';

if (!empty ($_POST['email']) && ($_POST['message'])) {
  mail($to, $subject, $message, $header);
echo "Thank you $name, your message has been received.";
exit;
}

if ( (empty ($_POST['email'])) && (empty ($_POST['message'])) ) {
echo "ERROR, you did not fill in the <b>Email</b> and the <b>message</b> body.";
exit;
}

elseif (empty ($_POST['email'])) {
echo "ERROR, you did not fill in your Email address.";
exit;
}

elseif (empty ($_POST['message'])) {
echo "ERROR, you did not fill in the message body.";
exit;
}

?>
于 2013-04-24T18:21:39.497 回答
1

我查看了您的网站,发现您的文件确实以 .php 结尾。我认为您的服务器尚未设置为将 .php 文件作为 php 执行。

您必须与服务器的管理员合作,以确保 .php 文件正在通过 php 处理程序运行。

配置的细节将取决于您的服务器平台......有很多网站可以引导您完成这些步骤。我不想特别推荐一个,因为我不知道你的出发点是什么。

于 2013-04-24T16:54:28.253 回答
0

请在下面查看我的 php 脚本。我已经设置了 SSMTP 并echo "hello " | mail -s "test" mail@gmail.com从 Ubuntu 命令行发送邮件。

<?php
/*
 *  CONFIGURE EVERYTHING HERE
 */

// an email address that will be in the From field of the email.
$from = 'Contact form <www.sabinamortgages.ca>';

// an email address that will receive the email with the output of the form
$sendTo = 'Sabina <sabina.kandik@premieremortgage.ca>';

// subject of the email
$subject = 'New message from contact form';

// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'surname' => 'Surname', 'need' => 'Need', 'email' => 'Email', 'message' => 'Message'); 

// message that will be displayed when everything is OK :)
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';

// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';

/*
 *  LET'S DO THE SENDING
 */


// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);

try
{

    if(count($_POST) == 0) throw new \Exception('Form is empty');

    $emailText = "You have a new message from your contact form\n=============================\n";

    foreach ($_POST as $key => $value) {
        // If the field exists in the $fields array, include it in the email 
        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    // All the neccessary headers for the email.
    $headers = array('Content-Type: text/plain; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
    );

    // Send email
    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}


// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
// else just display the message
else {
    echo $responseArray['message'];
}
于 2018-12-03T00:55:13.600 回答