2013-05-13 62 views
1

我想这样做,当用户点击页面底部的联系人按钮时,PHP脚本将检查邮件是否正确发送并显示一个面板基于状态。我已经实现了一个CSS类来显示面板,但这不起作用。在PHP中不通过JavaScript显示HTML/CSS面板

样品:

该脚本可以在AdamGinther.com

http://jsfiddle.net/gintherthegreat/ZFkmt/

<div id="contactme"> 
<p>Whether you’re just stopping to say hi or you have an inquiry, I enjoy receiving messages. For all of those employers out there, I am searching far and wide for a summer internship.</p> 
<br> <a href="https://www.facebook.com/whoisthecoolestpersonalive?ref=tn_tnmn"> 
<img src="images/facebook-512.png" height="50" width="50" alt="Facebook" target="_new"> 
</a> 

<br> 
<br> 
<form action="contact.php" method="post"> 
    <label name="firstName">Name:</label> 
    <input type="text" name="firstName"> 
    <br> 
    <br> 
    <label name="email">E-mail Address:</label> 
    <input type="text" name="email"> 
    <br> 
    <br> 
    <label name="message">Message:</label> 
    <textarea name="message" id="message"></textarea> 
    <br> 
    <br> 
    <input type="submit" value="Say Hello!" id="contactbutton"> 
</form> 
<div id="panel"> 
     <h1 id="output-inside"></h1> 

    <br> 
    <input type="button" alt="close" value="close" id="close-panel"> 
</div> 
<?php $field_firstName=$ _POST[ 'firstName']; $field_email=$ _POST[ 'email']; $field_message=$ _POST[ 'message']; $mail_to='[email protected]' ; $subject='AdamGinther.com message from ' .$field_firstName; $body_message='From: ' .$field_firstName. "\n"; $body_message .='E-mail: ' .$field_email. "\n"; $body_message .='Message: ' .$field_message; $headers='From: ' .$field_email. "\r\n"; $headers .='Reply-To: ' .$field_email. "\r\n"; $mail_status=m ail($mail_to, $subject, $body_message, $headers); if ($mail_status) { ?> 
<script language="javascript" type="text/javascript"> 
    $('#panel').show(); 
    $('#output-inside').text('Thank you ' + firstName + ', I will get back to you as soon as I can.'); 
</script> 
<?php } else { ?> 
<script language="javascript" type="text/javascript"> 
    $('#panel').show(); 
    $('#output-inside').text('I am sorry ' + firstName + ', but there was a problem processing your request. I can be contacted by e-mail at [email protected]'); 
</script> 
<?php } ?> 
找到210
+0

这里粘贴代码或创建的jsfiddle – dreamweiver 2013-05-13 06:09:01

+0

我检查您的网站尝试,我想你需要粘贴你的php代码以及更好的理解 – dreamweiver 2013-05-13 06:11:24

+0

所以我认为你正试图做一个页面弹出,而不是他们最终离开这个页面为您的PHP生成一个?如果是这种情况,您的表单不应该有任何操作。相反,您应该使用AJAX发送表单数据并在成功回调中显示面板。看看这个教程:http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/ – 2013-05-13 06:12:29

回答

0

尝试变化的行动页:

<form action="contact.php" method="post"> 

本身:

<form action="" method="post"> 

,因为这是你做了什么。

但是,您正试图按照mail();函数返回显示弹出消息。如果你使用Ajax,会更好。

教程:Send email using Ajax

0

使用jQuery生活事件在你的情况

http://api.jquery.com/live/

+0

'从jQuery 1.7开始,.live( )方法已被弃用。使用.on()附加事件处理程序。老版本的jQuery用户应该优先使用.delegate(),而不是.live()。'http://api.jquery.com/on/ – Mike 2013-05-20 21:10:03