duankezong4064 2013-07-11 21:07
浏览 20
已采纳

PHP表单无需打开新窗口

Good day/evening. I have this sort of form in HTML:

<form action="scripts/form-testimonial.php" class="form" method="post">
  <ul>
    <li><label for="name">Name</label> <input class="required" id="namef3" name="name" placeholder="Name" type="text"></li>
    <li><label for="email">E-mail</label> <input class="required" id="emailf3" name="email" placeholder="E-mail" type="text"></li>
    <li><label for="company">Company</label> <input id="companyf3" name="company" placeholder="Company (if applicable)" type="text"></li>
    <li><label for="msg">Message</label> <textarea class="required" cols="43" id="msgf3" name="msg" placeholder="Your feedback" rows="8"></textarea></li>
  </ul>
  <button class="submit" type="submit">Send your feedback</button>
</form>

and this sort of php sript for this form as a separate file:

<?php
  $receip = "office@avehire.co.uk";
  $subj = "Testimonial";
  $bodymsg = "Name: ".$_POST['name']."
"
               ."Company: ".$_POST['company']."
"
               ."Email: ".$_POST['email']."
"
               ."Message: ".$_POST['msg']."
";

      if(!$_POST['msg']){
             header("Location: ../error.html");
             exit;
      }

  $email = $_POST['email'];
  if(mail($receip, $subj, $bodymsg, 'From: Contact <'.$email.'>')){
  header("Location: ../msg_sent_feedback.html"); 
  }
?>

I also have a validator for this form, but this is not important. What is more important here is the way how this form works. Basically - when you click submit button - it goes to a separate php file, process the form and then - from what is already there - goes to msg_sent_feedback.html file. What I want to do is to NOT opens a separate file but just display a message in the same basic html file where the actual form exists. A message e.g. "Message sent".

Another thing - if I already have a validation script - do I need to have the first "if" in this form? or can I remove it totally? the one which redirects to error.html. Or can I remove these 4 lines? That line - I guess - is responsible for errors in the form, but as I mentioned, I have a validation script. I don't really want to change html. is it possible to modify the php script to work within the html document, as instructed above? thanks

  • 写回答

1条回答 默认 最新

  • duandi8613 2013-07-11 21:37
    关注

    Here is an Jquery ajax example that will help you.

    <?php
          $receip = "office@avehire.co.uk";
          $subj = "Testimonial";
          $bodymsg = "Name: ".$_POST['name']."
    "
                       ."Company: ".$_POST['company']."
    "
                       ."Email: ".$_POST['email']."
    "
                       ."Message: ".$_POST['msg']."
    ";
    
              if(!$_POST['msg']){
                     echo "<h1>Error sending e-mail";
                     exit;
              }
    
          $email = $_POST['email'];
          if(mail($receip, $subj, $bodymsg, 'From: Contact <'.$email.'>')){
            echo "<h1>Email sent successfully</h1>";
          }
        ?>
    

    Html :

    <!Doctype html>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    
    <script type="text/javascript">
    
    function submitmyform(){
        data=$('#myform').serialize();
            $.ajax({
            url: "myphpfile.php",
            type:'POST',
            data:data,
            async:false,
            dataType:'html',
            success: function(msg){
    
                $('#response_goes_here').html(msg);
    
            }
        });
    
    }
    
    </script>
    
        <form action="" id="myform">
    
            <ul>
                <li>    <label for="name">Name</label>              <input id="namef3" name="name" type="text" placeholder="Name" class="required"/>            
              </li>
                <li>    <label for="email">E-mail</label>           <input id="emailf3" name="email" type="text" placeholder="E-mail" class="required"/>        
              </li>
                <li>    <label for="company">Company</label>        <input id="companyf3" name="company" type="text" placeholder="Company (if applicable)" />   
              </li>
                <li>    <label for="msg">Message</label>            <textarea id="msgf3" name="msg" cols="43" rows="8" placeholder="Your feedback" class="required"></textarea> 
              </li>
            </ul>
            <button type="button" class="button" onclick="submitmyform();">Send your feedback</button>
        </form>
    
    <div id="response_goes_here"></div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)