PHP联系表格不起作用。获取内部服务器错误500

时间:2015-08-14 19:24:25

标签: php html forms email contact-form

所以我正在尝试为我的网站制作一个联系表格,但我不断收到内部服务器错误500.如果有人可以查看代码并告诉我,如果我做错了什么就会很好。谢谢!

HTML名称是index.html:

<form method="post" action="send_contact.php">
                        <p class="full-row">
                            <label for="name">Your Name:</label>
                            <input type="text" id="name" name="name">
                        </p>
                        <p class="full-row">
                            <label for="email">Email:</label>
                            <input type="text" id="email" name="email">
                        </p>
                        <p class="full-row">
                            <label for="subject">Subject:</label>
                            <input type="text" id="subject" name="subject">
                        </p>
                        <p class="full-row">
                            <label for="message">Message:</label>
                            <textarea name="message" id="message" rows="6"></textarea>
                        </p>
                        <input id="submit" class="mainBtn" type="submit" name="send" value="Send Message" onClick="">   
                    </form>

PHP名称为send_contact.php:

<?php
// Contact subject
$subject ="$subject"; 
// Details
$message="$detail";

// Mail of sender
$mail_from="$user_mail"; 
// From 
$header="from: $name <$mail_from>";

// Enter your email address
$to ="example@example.com";

$link = "<script>window.open('confirm.html')</script>";

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

// Check, if message sent to your email 
// display message "We've recived your information"
if($send_contact){
    header('confirm.html'); 
}
else {
echo "ERROR";
}
?>

3 个答案:

答案 0 :(得分:3)

要在PHP中获取变量值,请使用$ _POST [&#39; variableName&#39;]

例如获取此内容:

<input type="text" name="email">

你需要这个:

$email= $_POST['email']; 


<?php
// Contact subject
$subject = $_POST['subject']; 
// Details
$message=$_POST['message'];

// Mail of sender
$mail_from=$_POST['email']; 
// From 
$header="from: ".$_POST['name']." <".$_POST['email'].">";

// Enter your email address
$to ="example@example.com";

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

// Check, if message sent to your email 
// display message "We've recived your information"
if($send_contact){
    header('Location: confirm.html'); 
}    
else {
echo "ERROR";
}
?>

此处还有其他标题可帮助您的电子邮件进入收件箱而不是垃圾邮箱:

$to = "webtest@website.com";
$subject = "Test Email";
$message = "Test Email";

// normal headers
$num = md5(time()); 
$headers  = "From: Me <me@website.com>\r\n";
$headers  .= "MIME-Version: 1.0\r\n";
$headers  .= "Content-Type: multipart/mixed; ";
$headers  .= "boundary=".$num."\r\n";
$headers  .= "--$num\r\n";

// This two steps to help avoid spam   

$headers .= "Message-ID: <".time()." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";         

// With message

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n"; 


@mail($to, $subject, $message, $headers);

干杯!

答案 1 :(得分:0)

您正在发送带有post的表单,因此在php中您可以像$name = $_POST['name']一样访问它。

答案 2 :(得分:0)

您可以访问通过关联数组[$_POST][1]发送的数据,如下所示:

$subject = $_POST["subject"];

HTML:

<form method="post" action="send_contact.php">
    <p class="full-row">
        <label for="name">Your Name:</label>
        <input type="text" id="name" name="name"/>
    </p>
    <p class="full-row">
        <label for="email">Email:</label>
        <input type="email" id="email" name="email"/>
    </p>
    <p class="full-row">
        <label for="subject">Subject:</label>
        <input type="text" id="subject" name="subject"/>
    </p>
    <p class="full-row">
        <label for="message">Message:</label>
        <input type="text" name="message" id="message"/>
    </p>
    <input id="submit" class="mainBtn" type="submit" name="send" value="Send Message"/>   
</form>

PHP:     

//Check if the values requested were provided
if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["subject"]) && isset($_POST["message"])) {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $subject = $_POST["subject"];
    $message = $_POST["message"];

    $header = "from: $name <$email>";
    $to = "example@example.com";

    if (mail($to, $subject, $message, $header)) {
        header("Location: confirm.html");
    } else {
        echo("Error");
    }
} 
?>

正如其他人所建议的那样,如果您仍然遇到麻烦,请检查错误日志。