React 中有一个应用程序。有必要将表单中的数据发送到电子邮件。我不知道如何正确访问 php.ini 中已经准备好的文件。在控制台中使用 Ajax 时不断出现错误“POST ...sitename/mail.php 405”
如何正确访问php文件?我把它放在公共场所和文件本身旁边。
.php 文件:
<?php
$method = $_SERVER['REQUEST_METHOD'];
$c = true;
if ( $method === 'POST' ) {
$project_name = trim($_POST["project_name"]);
$admin_email = trim($_POST["admin_email"]);
$form_subject = trim($_POST["form_subject"]);
foreach ( $_POST as $key => $value ) {
if ( $value != "" && $key != "project_name" && $key != "admin_email" && $key != "form_subject" ) {
$message .= "
" . ( ($c = !$c) ? '<tr>':'<tr style="background-color: #f8f8f8;">' ) . "
<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
<td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
</tr>
";
}
}
} else if ( $method === 'GET' ) {
$project_name = trim($_GET["project_name"]);
$admin_email = trim($_GET["admin_email"]);
$form_subject = trim($_GET["form_subject"]);
foreach ( $_GET as $key => $value ) {
if ( $value != "" && $key != "project_name" && $key != "admin_email" && $key != "form_subject" ) {
$message .= "
" . ( ($c = !$c) ? '<tr>':'<tr style="background-color: #f8f8f8;">' ) . "
<td style='padding: 10px; border: #e9e9e9 1px solid;'><b>$key</b></td>
<td style='padding: 10px; border: #e9e9e9 1px solid;'>$value</td>
</tr>
";
}
}
}
$message = "<table style='width: 100%;'>$message</table>";
function adopt($text) {
return '=?UTF-8?B?'.Base64_encode($text).'?=';
}
$headers = "MIME-Version: 1.0" . PHP_EOL .
"Content-Type: text/html; charset=utf-8" . PHP_EOL .
'From: '.adopt($project_name).' <'.$admin_email.'>' . PHP_EOL .
'Reply-To: '.$admin_email.'' . PHP_EOL;
mail($admin_email, adopt($form_subject), $message, $headers );
}
用表单反应元素:
class FeedbackForm extends Component {
componentDidMount() {
if(FeedbackForm.defaultProps.prefferedLinkMethod !== this.props.prefferedLinkMethod)
this.setState({prefferedLinkMethod: this.props.prefferedLinkMethod})
//E-mail Ajax Send
$("form").submit(function() { //Change
console.log(this); //delete
var th = $(this);
$.ajax({
type: "POST",
url: "mail.php", //Change
data: th.serialize()
}).done(function(response) {
console.log("response: ", response)
alert("Thank you!");
setTimeout(function() {
// Done Functions
th.trigger("reset");
}, 1000);
}).catch((error) => {
console.log("Something went wrong with Ajax", error)
});
return false;
});
}
render() {
return(
<form>
{/* Hidden Required Fields*/}
<input type="hidden" name="project_name" value="name site" />
<input type="hidden" name="admin_email" value="email@email.com" />
<input type="hidden" name="form_subject" value="where form" />
{/* <!-- END Hidden Required Fields --> */}
<input type="text" name="name" />
<input type="text" name="telephone" />
<input type="text" name="email" />
<button >Отправить</button>
</form>
)
}
}
用户vanesicks实际上回答了这个问题。vercel主机不支持 .htaccess 文件。在那里,您需要使用 vercel.json 作为配置文件。这是有关此文档的链接:vercel.htaccess