gpt4 book ai didi

javascript - 使用 PHP 提交表单详细信息

转载 作者:行者123 更新时间:2023-12-03 06:46:54 26 4
gpt4 key购买 nike

我对 PHP 还很陌生。我必须将表格详细信息发送到邮件 ID。我浏览了互联网并获得了相同的各种链接。但我面临着类似的情况,当我提交包含详细信息的表单时,它正在浏览器中下载 PHP 文件,主要是我没有收到邮件。

这里我粘贴我的代码-

HTML 文件

<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<link rel="stylesheet" href="style.css">

</head>

<body>
<div id="page-wrapper">
<h1>AJAX Contact Form Demo</h1>
<div id="form-messages"></div>

<form id="ajax-contact" method="post" action="mailer.php">
<div class="field">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
</div>

<div class="field">
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
</div>

<div class="field">
<label for="message">Message:</label>
<textarea id="message" name="message" required></textarea>
</div>

<div class="field">
<button type="submit">Send</button>
</div>
</form>
<script src="jquery-2.1.0.min.js"></script>
<script src="app.js"></script>

</body>
</html>

mailer.php

<?php

// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);

// Check that data was sent to the mailer.
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "Oops! There was a problem with your submission. Please complete the form and try again.";
exit;
}

// Set the recipient email address.
//
$recipient = "shubhamvashishtha22@gmail.com";

// Set the email subject.
$subject = "New contact from $name";

// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";

// Build the email headers.
$email_headers = "From: $name <$email>";

// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}

} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}

?>

我的 app.js 文件

$(function() {
// Get the form.
var form = $('#ajax-contact');

// Get the messages div.
var formMessages = $('#form-messages');

// TODO: The rest of the code will go here...
});


// Set up an event listener for the contact form.
$(form).submit(function(event) {
// Stop the browser from submitting the form.
event.preventDefault();

// TODO
});

// Serialize the form data.
var formData = $(form).serialize();


// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
})
.done(function(response) {
// Make sure that the formMessages div has the 'success' class.
$(formMessages).removeClass('error');
$(formMessages).addClass('success');

// Set the message text.
$(formMessages).text(response);

// Clear the form.
$('#name').val('');
$('#email').val('');
$('#message').val('');
})

.fail(function(data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('success');
$(formMessages).addClass('error');

// Set the message text.
if (data.responseText !== '') {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text('Oops! An error occured and your message could not be sent.');
}
});

任何人都可以建议我如何解决这个问题。我实际上只是复制它而不是这种情况正在发生。请帮助我?

最佳答案

您的服务器可能没有启用 sendmailpostfixSMTP。您可以运行 phpinfo() 并查找此指令 sendmail_path 并查看设置的内容。

关于javascript - 使用 PHP 提交表单详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37697337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com