簡體   English   中英

HTML-PHP聯系人表格電子郵件

[英]HTML - PHP contact form email

我已經在這里和Google中搜索了如何制作聯系表格以進行工作,而我卻沒有能力。

我下載了“ php聯系人表格”,但我不知道如何使其工作。 我知道對此有很多疑問,但請幫幫我。 我被困在這里:/

這是HTML代碼:

<form action="form-to-email.php" name="myemailform" method="post">
    <div>
        <span>Name</span>
        <input type="text" name="name" value="" placeholder="Your Name">
    </div>

    <div>
        <span>Email</span>
        <input type="email" name="email" autocapitalize="off" autocorrect="off" value="" placeholder="example@domain.com">
    </div>

    <div><textarea name="message" placeholder="Message"></textarea></div>

    <div class="code">
        <button><input type="submit" name='submit' value="Send"></button>
    </div>
    <i class="clear" style="display: block"></i>
    </div>
</form>

這是我從網站下載的“免費PHP表單代碼”:

<?php
if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];

//Validate first
if(empty($name)||empty($visitor_email)) 
{
    echo "Name and email are mandatory!";
    exit;
}

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

$email_from = 'myEmail@domain.com';//<== update the email address
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
    "Here is the message:\n $message".

$to = "myEmail@domain.com";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

?> 

我需要在mysql中做某事還是要做更多其他事? 謝謝您的幫助!

一切看起來正確,只需在重定向后添加退出即可。

header('Location: thank-you.html');
exit;

在這里"Here is the message:\\n $message"."Here is the message:\\n $message". <=看到點了嗎? 那應該是一個分號。

這就是OP的代碼無法正常工作的原因。

錯誤報告添加到文件頂部,這將有助於查找可能存在的錯誤。

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:錯誤報告僅應在登台進行,而不應在生產過程中進行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM