我不擅长PHP(更多的是前端开发人员)而且我正在为客户做一个网站。除了一件事,我现在一切都在努力。当用户填写联系表单并成功提交时,我想显示一条消息,告诉他们电子邮件已发送。我已经设置了div,我只是不知道用PHP执行它。
<div class="sent">Email has been sent.</div>
<form action="" method="POST" class="contact-form">
<img class="close" src="images/close-icon.png">
<div class="top-title">Contact Us</div>
<input type="hidden" name="action" value="submit">
<div class="input-title">Name*</div>
<input class="input" name="name" type="text" value="" size="30" required><br>
<div class="input-title">Phone Number*</div>
<input class="input" name="phone" type="text" value="" size="30" required><br>
<div class="input-title">Email Address*</div>
<input class="input" name="email" type="text" value="" size="30" required><br>
<div class="input-title">How did you hear about us?</div>
<input class="input" name="company" type="text" value="" size="30"/><br>
<div class="input-title">Let us know how we can help you</div>
<textarea class="textarea" name="message" rows="7" cols="30"></textarea><br>
<div class="required">*Fill is required</div>
<input class="submit-button" type="submit" value="Send Email"/>
</form>
<?php
{
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$company=$_POST['company'];
$message= "\r\nName: " . $_POST['name'] ."\r\nPhone Number: ". $_POST['phone']."\r\nEmail: ". $_POST['email'] ."\r\nHow you know the company: ".$_POST['company'] ."\r\nReason for message: ".$_POST['message'];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="GS Website Message";
mail("name@email.com", $subject, $message, $from);
}
?>
答案 0 :(得分:2)
更改:(并使用条件语句)
mail("name@email.com", $subject, $message, $from);
为:
if(mail("name@email.com", $subject, $message, $from)){
$sent = "Message sent";
}
然后修改:
<div class="sent">Email has been sent.</div>
读作:
<div class="sent"><?php if(!empty($sent)) { echo $sent; } ?></div>
看到您是PHP /表单的新手,最好也检查您的输入是否为空,否则您可能无法获取邮件或接收空数据。
也在条件语句中设置。
I.e。:使用NOT empty:!
是PHP中的NOT运算符。
if(!empty($_POST['var'])){...}
修改强>
逻辑需要逆转。
首先,将PHP放在HTML表单上方。
我还在您的提交按钮中添加了submit
名称属性,并在div中添加了条件语句和ternary operator。
<?php
if(isset($_POST['submit'])){
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$company=$_POST['company'];
$message= "\r\nName: " . $_POST['name'] ."\r\nPhone Number: ". $_POST['phone']."\r\nEmail: ". $_POST['email'] ."\r\nHow you know the company: ".$_POST['company'] ."\r\nReason for message: ".$_POST['message'];
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="GS Website Message";
if(mail("email@example.com", $subject, $message, $from)){
$sent = "Message sent";
}
}
?>
<div class="sent"><?php echo isset($sent) ? $sent : ''; ?></div>
<form action="" method="POST" class="contact-form">
<img class="close" src="images/close-icon.png">
<div class="top-title">Contact Us</div>
<input type="hidden" name="action" value="submit">
<div class="input-title">Name*</div>
<input class="input" name="name" type="text" value="" size="30" required><br>
<div class="input-title">Phone Number*</div>
<input class="input" name="phone" type="text" value="" size="30" required><br>
<div class="input-title">Email Address*</div>
<input class="input" name="email" type="text" value="" size="30" required><br>
<div class="input-title">How did you hear about us?</div>
<input class="input" name="company" type="text" value="" size="30"/><br>
<div class="input-title">Let us know how we can help you</div>
<textarea class="textarea" name="message" rows="7" cols="30"></textarea><br>
<div class="required">*Fill is required</div>
<input class="submit-button" type="submit" value="Send Email" name="submit"/>
</form>
答案 1 :(得分:0)
$password=md5($_POST ['password']);
$password3 =md5($_POST ['password2']);
if($password != $password3)
{
$message = "Passwords do not match";
}
etc.
you need add
<form>
<div class="form-group">
<input type="password" name="password" id="password" class="form-control" placeholder="Password *" value="" required/>
</div>
<div class="form-group">
<div class="message"><?php if(!empty($message)) { echo $message; } ?></div>
<input type="password" name="password2" class="form-control" id="password2" placeholder="Confirm Password *" value="" required/>
</div>
</form>