我在这里有一个 PHP 联系表: leongaban.com
下面是一个屏幕截图,显示了我在单击提交后得到的 500(内部服务器错误)
HTML 表单
<div class="the-form">
<form id="contact">
<div id="status"></div>
<div>
<label for="name">Your Name</label>
<input type="text" name="name" id="name" value="" tabindex="1">
</div>
<div>
<label for="email">Your Email</label>
<input type="text" name="email" id="email" value="" tabindex="2">
</div>
<div>
<label for="textarea">Message:</label>
<textarea cols="40" rows="8" name="comments" id="comments" tabindex="3"></textarea>
</div>
<div>
<input class="submit-button" id="submit" type="submit" value="Submit" tabindex="5">
</div>
</form><!-- #contact -->
</div><!-- .the-form -->
我的 Javascript
$(function(){
$('#submit').click(function(){
var form_data = {
name: $('#name').val(),
email: $('#email').val(),
comments: $('#comments').val()
};
$.ajax({
url: "includes/send.php",
type: 'POST',
data: form_data,
success: function(status) {
if (status == 'success') {
$('#status').html('<h3 class="success">Thank You!</h3>')
.hide().fadeIn(2000);
} else {
$('#status').html('<p class="error">Both name and email are required fields.</p>')
.hide().fadeIn(2000);
}
} // end send contact form
}); //end ajax
return false;
}); //end send contact form click
});
我的 PHP
<?php
$name = $_POST['name'];
$email = trim($_POST['email']);
$comments = $_POST['comments'];
$error = array();
$site_owners_email = 'myEmail@gmail.com'; // Replace this with your own email address
$site_owners_name = 'Leon Gaban'; // replace with your name
if (strlen($name) < 2) {
$error['name'] = "Please enter your name.";
}
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
$error['email'] = "Please enter a valid email address.";
}
if (!$error) {
$response = 'success';
$subject = $name . ' needs help from CodePopper!';
$body = 'Codepopper,' . "\n\nName: " . $name .
"\nEmail: " . $email .
"\nComments: " . $comments;
require_once 'lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('hostname', 25)
->setUsername('username')
->setPassword('password')
;
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message->setSubject($subject);
$message->setFrom(array('myEmail@gmail.com' => 'leongaban.com'));
$message->setTo(array('myEmail@gmail.com' => 'Leon Gaban'));
$message->setBody($body);
$result = $mailer->send($message);
echo $response;
} # end if no error
else {
$response = 'error';
echo $response;
} # end if there was an error sending