我有 Apache 和 mySQL 运行,没有出现任何错误,因为我已经检查了所有日志;我的目标是在有新表单输入时向特定地址发送电子邮件
我对后端和 PHP 没有经验,所以我不太确定我哪里出了问题,任何帮助将不胜感激
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$checkbox = implode(', ', $_POST['checkbox']);
$message = $_POST['message'];
// Send email with form data
$to = '[email protected]';
$subject = 'New Contact Form Submission';
$body = "Name: $firstName $lastName\nEmail: $email\nCheckbox: $checkbox\nMessage: $message";
$headers = "From: $email";
if (mail($to, $subject, $body, $headers)) {
echo 'Message sent successfully';
} else {
echo 'Error sending message';
}
}
?>
我按照建议将此文件保存在我的 XAMPP 安装文件夹的 htdocs 文件夹中
我将 php 文件输入到我的反应项目中:
axios.post('http://localhost/mihcontactform.php', formData)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
我正在尝试对此进行测试(我创建了一个带有 echo Hello World 的虚拟 php 文件,它出现了,所以没有任何配置问题)但是当我尝试访问 localhost/mihcontactform.php 时,只出现了一个白色的空白页面
供参考,这是我从中获取数据的方式,我在 javascript 中创建的表单:
import { useState } from 'react';
import axios from 'axios';
function ContactForm() {
const [formData, setFormData] = useState({
firstName: '',
lastName: '',
email: '',
checkbox: [],
message: ''
});
const handleFormSubmit = (e) => {
e.preventDefault();
const form = e.target;
const formData = new FormData(form);
axios.post('http://localhost/mihcontactform.php', formData)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
const handleInputChange = (e) => {
if (e.target.type === 'checkbox') {
let checkboxValues = formData.checkbox.slice();
if (e.target.checked) {
checkboxValues.push(e.target.value);
} else {
checkboxValues.splice(checkboxValues.indexOf(e.target.value), 1);
}
setFormData({
...formData,
checkbox: checkboxValues,
});
} else {
setFormData({
...formData,
[e.target.name]: e.target.value,
});
}
};
return (
<form onSubmit={handleFormSubmit} method='POST' action='mihcontactform.php'>
<label>
First Name:
<input type='text' name='firstName' value={formData.firstName} onChange={handleInputChange} required/>
</label>
<label>
Last Name:
<input type='text' name='lastName' value={formData.lastName} onChange={handleInputChange} required/>
</label>
<label>
Email:
<input type='email' name='email' value={formData.email} onChange={handleInputChange} required/>
</label>
<label>
What services are you contacting us about?
<input type='checkbox' name='checkbox[]' value='A' onChange={handleInputChange} required/> A
<input type='checkbox' name='checkbox[]' value='B' onChange={handleInputChange} required/> B
<input type='checkbox' name='checkbox[]' value='C' onChange={handleInputChange} required/> C
<input type='checkbox' name='checkbox[]' value='D' onChange={handleInputChange} required/> D
</label>
<label>
Message:
<textarea name='message' value={formData.message} onChange={handleInputChange} required></textarea>
</label>
<button type='submit'>Submit</button>
</form>
);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
}
这部分显然不是真正的编辑:不妨检查 $_POST,以及每个 $_POST[''] 是否为空
如果是用户提交的代码,还要小心使用未经处理的 $_POST 直接进入代码。
另外,小心 inbuild php mail() 它有点奇怪并且有一些怪癖