简体   繁体   English

PHP联系表单脚本将不会运行/打开页面

[英]PHP contact form script won't run/ page opens instead

I'm stuck and I hope someone can help me. 我被困住了,我希望有人可以帮助我。 I'm trying to add a simple contact form to my website using PHP action=" " method="post". 我正在尝试使用PHP action =“”method =“post”向我的网站添加一个简单的联系表单。 When I click the "submit" button, instead of running the script, the "send_contact.php" page opens and you see all the code. 当我单击“提交”按钮而不是运行脚本时,将打开“send_contact.php”页面,您将看到所有代码。 I've checked and tested my server (blue.host) and it is set up to run php scripts. 我检查并测试了我的服务器(blue.host),它被设置为运行php脚本。 I've tried everything and I'm at a loss. 我已经尝试了一切,但我很茫然。 Here is the HTML: 这是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-->                       

Here's the PHP script: 这是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";
}
?>

Here's the CSS: 这是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;
}

Here the actual page from my website: http://jacineariasdesign.jacineariasweb.com/contact.html Any help to make this work would be appreciated! 这里是我网站上的实际页面: http//jacineariasdesign.jacineariasweb.com/contact.html任何帮助使这项工作将不胜感激! Thank you! 谢谢!

I looked at your site, and see that your file does end in .php. 我查看了您的网站,看到您的文件以.php结尾。 I think that your server has not been set up to execute .php files as php. 我认为您的服务器尚未设置为以php身份执行.php文件。

You'll have to work with the administrator of your server to make sure that .php files are being run through the php handler. 您必须与服务器的管理员一起工作,以确保通过php处理程序运行.php文件。

The specifics of the configuration are going to depend on your server platform ... There are a lot of sites out there to walk you through the steps. 配置的细节取决于您的服务器平台......有很多站点可以指导您完成这些步骤。 I don't want to recommend one specifically because I don't know what your starting point is. 我不想特别推荐一个,因为我不知道你的出发点是什么。

Here, give this a try. 在这里,尝试一下。 Tested and could stand to be modified with extra security features, but it works with your form supplied. 经过测试,可以使用额外的安全功能进行修改, 但它适用于您提供的表单。

Added a bit of error checking also. 还添加了一些错误检查。

If it doesn't work for you, then you have a PHP issue on "your" server. 如果它不适合您,那么您的“您的”服务器上有PHP问题。

Your original PHP script never had a Subject entry to start with. 您的原始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;
}

?>

please see my php script below. 请看下面我的PHP脚本。 I have set up SSMTP and echo "hello " | mail -s "test" mail@gmail.com 我已经设置了SSMTP并echo "hello " | mail -s "test" mail@gmail.com echo "hello " | mail -s "test" mail@gmail.com from Ubuntu command line sends mail. 来自Ubuntu命令行的echo "hello " | mail -s "test" mail@gmail.com发送邮件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM