在同一个div标签内显示echo消息

时间:2014-04-07 12:28:32

标签: javascript php html database forms

我正在制作联系表格,将表格数据发送到数据库以及邮件(工作正常)但是在提交表格时,我想在.mail_middle类中显示感谢信息并隐藏表格。

现在我收到消息“谢谢你的评论!”而不是在同一个班级中“谢谢你的报价请求”。

我希望“感谢您引用请求”以显示并隐藏表单。

Plz帮助我进一步使用代码,因为我不知道现在要做什么.....

form.php的

<form method='post'>
  Name: <input type='text' name='name' id='name' />
  Email: <input type='text' name='email' id='email' />
  <input type='submit' value='Submit' class='mailbtn' />
</form>

process_db.php

<?php

  [...]    
  mysql_query($query);    
  echo "Thank you for your Comment!";    
  mysql_close($con);      
  include("mail.php"); 
}

HTML

<div class="middle mail_middle">

<fieldset>
    <?php include("form.php"); ?>
    <?php include("process_db.php"); ?>                                         
</fieldset>

</div>

<script type="text/javascript">

            $('.mailbtn').live('click',function(){

            $.ajax({
            type: "POST",
            async : false,
            url: "mail.php",
            data: { name:name, email:email, phn:phn, budget:bgt, comment:txt, loc:loc}

            })
            .done(function( msg ) {
            $('.mail_middle').html('');
            $('.mail_middle').html('Thank you for quote request.');
            return false;
            });


    });
</script>

mail.php

<?php

$to =  array("email-id1","email-id2");

$subject = "my_subject";
$message .= "<table border='1'>";
$message .= "<tr><td>Name    </td><td>".$_POST['name']."</td></tr>";
$message .= "<tr><td>Email   </td><td>".$_POST['email']."</td></tr>";
$message .= "</table>";

$from = "email-id";
$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/html; charset=utf-8\r\n"; 
$headers .=  'from: '.$from .'' . "\r\n" .

            'Reply-To: '.$from.'' . "\r\n" .

            'X-Mailer: PHP/' . phpversion();
foreach($to as $row)
{
   mail($row,$subject,$message,$headers);
}

echo "Mail Sent.";
die;
?>

1 个答案:

答案 0 :(得分:1)

在你的jQuery .done()回调中,我不确定在这里返回false的效果是什么,但从我所看到的,AJAX方法很可能不会被调用。

如果您的按钮未收到来自click事件的错误回复,则属于这种情况。

我建议您将代码更改为此...

<强> form.php的

<form method='post' class="mailform">
     Name: <input type='text' name='name' id='name' />
     Email: <input type='text' name='email' id='email' />
     <input type='submit' value='Submit' class='mailbtn' />
</form>

<强> process_db.php

<?php

  [...]    
  mysql_query($query);    
  echo "Thank you for your quote request.";    
  mysql_close($con);      
  include("mail.php"); 
  $myformsubmitted = true;
}

<强> html.php

<div class="middle mail_middle">

    <fieldset>
        <?php include("process_db.php"); ?> 
        <?php if(!$myformsubmitted) include("form.php"); ?>                                        
    </fieldset>

</div>

<script type="text/javascript">

    // if js is supported submit by ajax...
    $('.mailform').live('submit', function(event){

        event.preventDefault();

        $.ajax({
            type: "POST",
            async : false,
            url: "process_db.php",
            data: { 
                name:name, 
                email:email, 
                phn:phn, 
                budget:bgt, 
                comment:txt, 
                loc:loc
            }
        })
        .done(function( msg ) {
            $('.mail_middle').html('Thank you for quote request.');
        });


    });
</script>