我根据答案更新了以下代码
完全披露我不懂PHP。我需要一个联系表单,我理解下面这段代码足以让我感觉舒服。但是当我尝试提交联系表单时,我收到一个错误: POST http://(myurlhere)/contact_form.php 500(内部服务器错误)
我很欣赏任何见解。以下是代码:
HTML
<form id="form">
<p id="returnmessage"></p>
<input type="text" id="name" placeholder="Name"/>
<input type="text" id="email" placeholder="Email"/>
<textarea id="message" placeholder="Your Message Here"></textarea>
</form>
<a id="submit">Send</a>
PHP
<?php
// Fetching Values from URL.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
// After sanitization Validation is performed
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// if (!preg_match("/^[0-9]{10}$/", $contact)) {
// echo "<span>* Please Fill Valid Contact No. *</span>";
} else {
$subject = $name;
// To send HTML mail, the Content-type header must be set.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n"; // Sender's Email
$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>I will be in touch soon.<br/><br/>'
. 'Name:' . $name . '<br/>'
. 'Email:' . $email . '<br/>'
. 'Message:' . $message . '<br/><br/>'
. 'This is a Contact Confirmation mail.'
. '<br/>'
. 'We Will contact You as soon as possible .</div>';
$sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" . $template . "</div>";
// Message lines should not exceed 70 characters (PHP rule), so wrap it.
$sendmessage = wordwrap($sendmessage, 70);
// Send mail by PHP Mail Function.
mail("myemailaddresshere@gmail.com", $subject, $sendmessage, $headers);
echo "I will be in touch soon.";
}
} else {
echo "<span>* invalid email *</span>";
}
?>
新代码 - 它会发送邮件,但邮件中不包含姓名,电子邮件或邮件。
新HTML
<form id="form" method="post" action="">
<p id="returnmessage"></p>
<input type="text" id="name" name="name" placeholder="Name"/>
<input type="text" id="email" name="email" placeholder="Email"/>
<textarea id="message" name="message" placeholder="Message"></textarea>
<button id="submit" id="submit">Send</button>
</form>
新PHP
<?php
// Fetching Values from URL.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
// After sanitization Validation is performed
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// if (!preg_match("/^[0-9]{10}$/", $contact)) {
// echo "<span>* Please Fill Valid Contact No. *</span>";
} else {
$subject = $name;
// To send HTML mail, the Content-type header must be set.
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n"; // Sender's Email
$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>I will be in touch soon.<br/><br/>'
. 'Name:' . $name . '<br/>'
. 'Email:' . $email . '<br/>'
. 'Message:' . $message . '<br/><br/>'
. 'This is a Contact Confirmation mail.'
. '<br/>'
. 'We Will contact You as soon as possible .</div>';
$sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" . $template . "</div>";
// Message lines should not exceed 70 characters (PHP rule), so wrap it.
$sendmessage = wordwrap($sendmessage, 70);
// Send mail by PHP Mail Function.
mail("myemailhere@gmail.com", $subject, $sendmessage, $headers);
echo "I will be in touch soon.";
}
?>
答案 0 :(得分:1)
您提交的代码似乎存在问题。我发现有些部分存在问题
表单方法并提交请求到php没有设置,它必须是这样的
<form id="form" method="post" action="php-url">
在结束</form>
之后添加提交链接,因此这个链接必须在表单结束之前
<button type"submit" id="submit">Send</a>
在你的php文件中,在else语句之前添加了double}}
}
} else {
echo "<span>* invalid email *</span>";
最终的HTML会像
<form id="form" method="post" action="submit.php">
<p id="returnmessage"></p>
<input type="text" id="name" name="name" placeholder="Name"/>
<input type="email" id="email" name="email" placeholder="Email"/>
<textarea id="message" name="message" placeholder="Your Message Here"></textarea>
<button type="submit" id="submit">Send</a>
</form>
PHP
<?php
// Fetching Values from URL.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
$subject = $name;
// To send HTML mail, the Content-type header must be set.
$recipient = "myemailaddresshere@gmail.com";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From:' . $email. "\r\n"; // Sender's Email
$headers .= 'Cc:' . $email. "\r\n"; // Carbon copy to Sender
$template = '<div style="padding:50px; color:white;">Hello ' . $name . ',<br/>'
. '<br/>I will be in touch soon.<br/><br/>'
. 'Name:' . $name . '<br/>'
. 'Email:' . $email . '<br/>'
. 'Message:' . $message . '<br/><br/>'
. 'This is a Contact Confirmation mail.'
. '<br/>'
. 'We Will contact You as soon as possible .</div>';
$sendmessage = "<div style=\"background-color:#7E7E7E; color:white;\">" . $template . "</div>";
// Message lines should not exceed 70 characters (PHP rule), so wrap it.
$sendmessage = wordwrap($sendmessage, 70);
// Send mail by PHP Mail Function.
mail($recipient, $subject, $sendmessage, $headers);
echo "I will be in touch soon.";
?>
检查证明
答案 1 :(得分:0)
我可以看到一些问题:
1。更改您的表单,使用PHP期望的名称添加name
属性。
2。在表单中添加一个提交按钮,并确保它实际位于表单中(您的<a>Send</a>
在表单之外)
3。更改您的表单以使用POST
代替GET
方法,因为您的PHP脚本正在$_POST
4. 由于您需要输入所有输入邮件,因此请为其添加required
属性
以下表格进行全部4次调整
<form id="form" method="post">
<p id="returnmessage"></p>
<input type="text" id="name" name="name" placeholder="Name" required/>
<input type="text" id="email" name="email" placeholder="Email" required/>
<textarea id="message" name="message" placeholder="" required></textarea>
<button type="submit" name="submit">Send</button>
</form>
如果处理提交的脚本与表单不是同一个文件,您还必须向action
添加<form>
属性。
5. 在PHP端,在尝试使用之前检查输入是否可用。现在,如果没有提交任何内容,您的代码将继续使用不存在的值
<?php
if(isset($_POST['email'],$_POST['name'],$_POST['message'])){
// proceed with your regular script here
}else{
// no form values were submitted, so nothing to email
}
6. 找出服务器错误日志的位置并进行审核。除了我的步骤,可能还需要进行更多更改。可以在错误日志中找到500 error
的详细信息。
答案 2 :(得分:0)
请尝试以下代码。
HTML代码
<form id="form" method="post" action="">
<p id="returnmessage"></p>
<input type="text" name="name" placeholder="Name"/>
<input type="text" name="email" placeholder="Email"/>
<textarea name="message" placeholder="Your Message Here"></textarea>
<input type="submit" value="Submit"/>
</form>
PHP代码
if(!empty($_POST))
{
// Fetching Values from URL.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing E-mail.
// After sanitization Validation is performed
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
//check email validation and show error
} else {
//Email sending functionality
}
}
答案 3 :(得分:0)
您是否可以从服务器发送任何网络邮件? 这是一个php脚本,我称之为万无一失,它非常小,如果它无法发送电子邮件,则您可能会有某种Web邮件限制。这适用于我的共享服务器。我还建议您检查服务器日志。
<?php
function sanitize_my_email($field) {
$field = filter_var($field, FILTER_SANITIZE_EMAIL);
if (filter_var($field, FILTER_VALIDATE_EMAIL)) {
return true;
} else {
return false;
}
}
$to_email = 'your-email@your-domain.com';
$subject = 'PHP Foolproof Send Mail script';
$message = 'This mail is sent using the foolproof PHP mail script';
$headers = 'From: gallienus@roman-empire.com';
//check if the email address is invalid $secure_check
$secure_check = sanitize_my_email($to_email);
if ($secure_check == false) {
echo "Invalid input";
}
else { //send email
mail($to_email, $subject, $message, $headers);
echo "echo: This email is sent using simple foolproof PHP mail";
}
?>