有人可以帮我找一下代码有什么问题吗?实际上我正在尝试将表单数据直接发送到邮件ID,但以下代码不起作用。我需要以表格格式将所有用户输入数据发送到邮件ID。
HTML部分
<div class="form-group">
<label class="control-label col-lg-2" for="Patient">Patient:</label>
<div class="col-lg-10">
<input type="text" class="form-control fc" name="patient" id="Patient">
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="Date">Date:</label>
<div class="col-lg-10">
<input type="date" class="form-control fc" name= "date" id="Date">
</div>
</div>
<button type="submit" class="btn btn-default" data-loading-text="Sending...">Send</button>
PHP
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$userinput==true; // set trigger for verification
//Error variables
$patientErr1="";
$patientErr2="";
$DateErr = "";
$DoctErr = "";
$patient = $_POST["patient"];
$date = $_POST["date"];
if(isset($_POST['submit'])){
if(empty($patient)){
$patientErr1 = "You have to provide Name";
$userinput = false;
}
if (!preg_match("/^[a-zA-Z ]*$/",$patient)){
$patientErr2 = "You can't provide numeric value in name field";
$userinput = false;
}else{
$patient = test_input($patient);
}
if (empty($date)){
$DateErr = "Please select date";
$userinput = false;
}else{
$date = test_input($date);
}
if(empty($doctor)){
$DoctErr = "Please fill Doct Name";
$userinput = false;
}
if (!preg_match("/^[a-zA-Z ]*$/",$doctor)){
$DoctErr2 = "You can't provide numeric value in name field";
$userinput = false;
}else{
$patient = test_input($doctor);
}
if ($userinput == true ){
// mail will sent to
$to = "test12345@gmail.com";
$subject = "User input";
$message = "
?>
<html>
<head>
</head>
<body>
<table>
<tr>
<th>Field Name</th>
<th>Value</th>
</tr>
<tr>
<td>patient Name</td>
<td><?php echo $patient; ?></td>
</tr>
<tr>
<td>date</td>
<td><?php echo $doctor; ?></td>
</tr>
</table>
</body>
</html>
<?php";
}
//send mail
mail($to,$subject,$message);
?>
答案 0 :(得分:0)
如果你想使用php mail()函数以html格式发送电子邮件,那么你需要设置标题并将内容类型设置为html。
你的脚本中有很多问题,你的php有_POSTs,你没有在你的表单中,我已经完成了你的整个脚本。请检查我和你的,看看你出错了然后从中学习。
谢谢:)
<?php
$errors = "";
; // check number of errors
//Error variables
$patientErr1 = "";
$DateErr = "";
$DoctErr = "";
$emailErr = "";
$DoneMessage = "";
$to = "test12345@gmail.com";
$subject = "User input";
if (isset($_POST['submit'])) {
if (empty($_POST['patient'])) {
$patientErr1 = "You have to provide Name";
$errors++;
} else {
$patient = test_input($_POST['patient']);
if (!preg_match("/^[a-zA-Z ]*$/", $patient)) {
$patientErr1 = "You can't provide numeric value in name field";
$errors++;
}
}
if (empty($_POST['date'])) {
$DateErr = "Please select date";
$errors++;
} else {
$date = test_input($_POST['date']);
}
if (empty($_POST['email'])) {
$emailErr = "enter email";
$errors++;
} else {
$email = test_input($_POST['email']);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
$emailErr = "Enter valid email";
}
}
if ($errors > 0) {
// We have errors print them back
$DoneMessage = "Please fix " . $errors . " below to send message";
} else {
// No Errors set headers and send email
$headers = "MIME-Version: 1.0" . "\r\n"; // set html
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: ' . $patient . ' <' . $email . '>' . "\r\n";
$message = "<table>
<tr>
<th>Patient Name</th>
<th>Value</th>
<th>Email</th>
</tr>";
$message .= "<tr>
<td>$patient</td>
</tr>";
$message .= "<tr>
<
<td>$date</td>
</tr>";
$message .= "<tr>
<td>$email</td>
</tr>";
$message . "= </table>";
if (mail($to, $subject, $message, $headers)) {
$DoneMessage = "Thaks email sent";
} else {
$DoneMessage = "Server problem could not send email";
}
}
// mail will sent to
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php echo $DoneMessage;?>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="form-group">
<label class="control-label col-lg-2" for="Patient">Patient: <?php echo $patientErr1;?></label>
<div class="col-lg-10">
<input type="text" class="form-control fc" name="patient" id="Patient">
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="Date">Date: <?php echo $DateErr;?></label>
<div class="col-lg-10">
<input type="date" class="form-control fc" name= "date" id="Date">
</div>
</div>
<div class="form-group">
<label class="control-label col-lg-2" for="email">Your email: <?php echo $emailErr;?></label>
<div class="col-lg-10">
<input type="email" class="form-control fc" name="email" id="email">
</div>
</div>
<button type="submit" name="submit" class="btn btn-default" data-loading-text="Sending...">Send</button>
</form>