React提交的联系人表单Form未通过PHP后台发送电子邮件
class Contact extends React.Component { constructor(props) { super(props); this.state = { name: '', email: '', message: '' } } render() { return ( <> <Helmet> <title>Title</title> </Helmet> <h2>Contact</h2> <form id="contact-form" onSubmit={this.handleSubmit.bind(this)} method="POST"> <div className="form-group"> <input type="text" className="form-control" placeholder="Name" /> </div> <div className="form-group"> <input type="email" className="form-control" aria-describedby="emailHelp" placeholder="Email" /> </div> <div className="form-group"> <textarea className="form-control" rows="5" placeholder="Message" ></textarea> </div> <button type="submit" className="btn btn-primary">Submit</button> </form> </> ) } onNameChange(event) { this.setState({name: event.target.value}) } onEmailChange(event) { this.setState({email: event.target.value}) } onMessageChange(event) { this.setState({message: event.target.value}) } handleSubmit(e) { e.preventDefault(); fetch('/mail.php', { method: "POST", body: JSON.stringify(this.state), headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, }).then( (response) => (response.json()) ).then((response)=> { if (response.status === 'success') { alert("Message Sent."); this.resetForm() } else if(response.status === 'fail') { alert("Message failed to send.") } }) }
提交联系表单时,我收到一个后台返回的错误 Message failed to send.
该文件mail.php
位于的根目录中public_html
。该文件与案例相同,并且包含:
<?php header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: Content-Type"); header("Content-Type: application/json"); $rest_json = file_get_contents("php://input"); $_POST = json_decode($rest_json, true); $errors = array(); if ($_SERVER['REQUEST_METHOD'] === "POST") { if (empty($_POST['email'])) { $errors[] = 'Email is empty'; } else { $email = $_POST['email']; // validating the email if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errors[] = 'Invalid email'; } } if (empty($_POST['message'])) { $errors[] = 'Message is empty'; } else { $message = $_POST['message']; } if (empty($errors)) { $date = date('j, F Y h:i A'); $emailBody = " <html> <head> <title>$email is contacting you</title> </head> <body style=\"background-color:#fafafa;\"> <div style=\"padding:20px;\"> Date: <span style=\"color:#888\">$date</span> <br> Email: <span style=\"color:#888\">$email</span> <br> Message: <div style=\"color:#888\">$message</div> </div> </body> </html> "; $headers = 'From: Contact Form <contact@mydomain.com>' . "\r\n" . "Reply-To: $email" . "\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: text/html; charset=iso-8859-1\r\n"; $to = 'contact@example.com'; $subject = 'Contacting you'; if (mail($to, $subject, $emailBody, $headers)) { $sent = true; } } } ?> <?php if (!empty($errors)) : ?> { "status": "fail", "error": <?php echo json_encode($errors) ?> } <?php endif; ?> <?php if (isset($sent) && $sent === true) : ?> { "status": "success", "message": "Your data was successfully submitted" } <?php endif; ?>
有人能帮我确定为什么提交的表给未成功发送电子邮件吗?
密码猴子的回答
根据案例,应该控制您的输入元素(即应该为值prop分配一个状态变量),并onChange
附加一个事件:
<div className="form-group"> <label htmlFor="name">Name</label> <input type="text" className="form-control" value={this.state.name} onChange={this.onNameChange.bind(this)} /> </div> <div className="form-group"> <label htmlFor="exampleInputEmail1">Email address</label> <input type="email" className="form-control" aria-describedby="emailHelp" value={this.state.email} onChange={this.onEmailChange.bind(this)} /> </div> <div className="form-group"> <label htmlFor="message">Message</label> <textarea className="form-control" rows="5" value={this.state.message} onChange={this.onMessageChange.bind(this)} /> </div>其他状态未更新,您的提交将空字段发送到您的php文件。