如果发送了电子邮件,则隐藏联系表格div

时间:2013-08-12 09:38:25

标签: php html show-hide

我在div上有一个联系表单,我只想在邮件发送后隐藏div。 我的下面的代码没有隐藏联系表单

<?php 
if(mail($to,$subject,$body,$headers)) 
    { 
    echo "<script type='text/javascript'>"; 
    echo "hideDiv();" 
    echo "</script>"; 
    $msg="We have received your enquiry. We will contact you soon."; 
    } 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<script type="text/javascript"> 
function hideDiv() 
{ 
document.getElementById("contact_form_div").style.display="none"; 
} 
</script> 
</head> 

<body> 
<form onsubmit="return validate()"> 
<!-- contact form content--> 
<input type="submit" value="submit" name="submit" /> 
</form> 
</body> 
</html>

2 个答案:

答案 0 :(得分:0)

您的代码中没有div ...您需要识别要隐藏的表单=&gt;添加ID

<form onsubmit="return validate()" id="myID">

或者添加一个包装表单的div

<div id="myID">
  <form onsubmit="return validate()"> 
    <!-- contact form content--> 
    <input type="submit" value="submit" name="submit" /> 
  </form>
</div>

然后通过选择ID隐藏表单或div:

document.getElementById("myID").style.display="none"; 

答案 1 :(得分:0)

如果某人关闭了javascript会怎么样?为什么不决定用if - else在php中显示什么?

<?php
if(mail($to,$subject,$body,$headers)) 
{
    echo "We have received your enquiry. We will contact you soon."; 
} 
else
{
?>
    <form onsubmit="return validate()"> 
        <!-- contact form content--> 
        <input type="submit" value="submit" name="submit" /> 
    </form> 
<?php
}
?>