PHP联系人表单脚本赢得';t运行/页面将打开

PHP联系人表单脚本赢得';t运行/页面将打开,php,contact-form,Php,Contact Form,我陷入困境,希望有人能帮助我。我正在尝试使用PHP action=“”method=“post”向我的网站添加一个简单的联系人表单。当我单击“提交”按钮时,将打开“send_contact.php”页面,而不是运行脚本,您将看到所有代码。我已经检查并测试了我的服务器(blue.host),它被设置为运行php脚本。我什么都试过了,现在不知所措。以下是HTML: <div class="form"> <form id="myform" name="myform" action="

我陷入困境,希望有人能帮助我。我正在尝试使用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格式执行.php文件

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


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

在这里,尝试一下。经过测试,可以通过额外的安全功能进行修改,但它可以与您提供的表单一起工作

还添加了一点错误检查

如果它不适合您,那么“您的”服务器上就会出现PHP问题。

您的原始PHP脚本从来没有一个
主题
条目作为开始

<?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;
}

?>

请参见下面我的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'];
}

只是为了确保php正在运行和工作,请尝试放置phpinfo();在php文件中,并在浏览器中打开它。首先,运行URL时,整个脚本都是
注释的
。例如,
//From$header=“From:$name”所以,它当然不会起作用。删除所有
/
。从主机上查看这篇文章:@Fred,不是它,只是它没有任何HTML中断,所以看起来像那样。查看源代码。@Fred-这会导致错误,但我无法想象它会强制打印整个脚本。OP真的应该尝试一个带有
的简单文件或一些基本的东西,看看是脚本还是他的服务器。谢谢大家!我将属性改为,然后我将PHP改为类似于Fred建议的东西,它成功了!我感谢所有的帮助!:)寻找任何我能得到的帮助,我只是非常困,非常新的这一点
<?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;
}

?>
<?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'];
}