我正在运行 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] /cdn-cgi/l/email-protection';
$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 文件输入到我的 React 项目中,如下所示:
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>
);
}
这些是当我使用 $_GET 而不是 $_POST 时出现的错误