我遇到了这段代码的问题。提交填写的表单后,显示消息(Email sent!)不会显示在提交按钮旁边。我需要在联系表单上的“提交”按钮旁边显示“电子邮件已发送”消息。提交表单后,即使刷新或返回操作后,我还需要清除表单字段。请帮我解决这个代码
表格动作图片:
width
答案 0 :(得分:0)
您可以在发送电子邮件后重定向页面,也可以设置$msg='email sent';
之类的变量,并在提交按钮之前调用此变量。
$action=$_REQUEST['action'];
if ($action!="") /* display the contact form */
{
$your_name = $_REQUEST['your_name'];
$email = $_REQUEST['email'];
$mobile_number = $_REQUEST['mobile_number'];
$message = $_REQUEST['message'];
$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";
$recipient = "name@email.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (($your_name=="")||($email=="")||($message==""))
{
header('location: yoururl?msg=error');
//$msg = "All fields are required, please fill <a href=\"\">the form</a> again.";
}
else{
@mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('location: yoururl?msg=succss');
//$msg = 'Email Sent!';
}
}
?>
<form name="contactform" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<label for="your_name">Your Name <font color="red">*</font></label>
<input type="text" id="reset" name="your_name" placeholder="Enter Your Name" maxlength="20" size="40" >
<label for="email">Email Address <font color="red">*</font></label>
<input type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" required>
<label for="mobile_number">Mobile Number</label>
<input type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" >
<label for="message">Message <font color="red">*</font></label>
<textarea name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>
<?php if(isset($_REQUEST['msg']) and $_REQUEST['msg']=='success'){echo "Email sent!";}if(isset($_REQUEST['msg']) and $_REQUEST['msg']=='error'){echo "All fields are required, please fill <a href=\"\">the form</a> again.";} ?>
<?php echo $msg; ?>
<input type="submit" value="Submit">
</form>
答案 1 :(得分:0)
您的代码应该是这样的。请注意isset($_POST['submit']
,$msg
,value=""
。
<?php
if(isset($_POST['submit']))
{
$your_name = $_REQUEST['your_name'];
$email = $_REQUEST['email'];
$mobile_number = $_REQUEST['mobile_number'];
$message = $_REQUEST['message'];
$formcontent="From: $your_name \n Email: $email \n Phone Number: $mobile_number \n Message: $message";
$recipient = "name@email.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
if (($your_name=="")||($email=="")||($message==""))
{
$msg = "All fields are required";
}
else{
@mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
$msg = "Email Sent";
}
}
?>
<form name="contactform" method="post" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
<label for="your_name">Your Name <font color="red">*</font></label>
<input type="text" id="reset" name="your_name" placeholder="Enter Your Name" maxlength="20" size="40" value="">
<label for="email">Email Address <font color="red">*</font></label>
<input type="email" id="reset" name="email" placeholder=" Enter Your E-mail Address" maxlength="20" size="40" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" required value="">
<label for="mobile_number">Mobile Number</label>
<input type="tel" id="reset" name="mobile_number" pattern="[0-9]{1}[0-9]{9}" placeholder="Enter Your Phone Number by Adding Country Code (eg: +91.,)" maxlength="30" size="40" value="">
<label for="message">Message <font color="red">*</font></label>
<textarea name="message" id="reset" placeholder="Your Message Goes Here" maxlength="1000" cols="62" rows="10" required></textarea>
<input type="submit" name="submit" value="Submit">
<?php echo $msg; ?>
</form>