-1

表单操作是“”,因为它都使用相同的页面。无论如何表单可以在同一页面上提交而不刷新?

PHP:

<?php 
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    $to="josue.espinosa@mtchs.org";
    $subject="Tech Muffins Contact";
    $message="Name:      ".$_REQUEST['name']."\n\nEmail:      ".$_REQUEST['email']."\n\nMessage: ".$_REQUEST['message'];
    $from = "donotreply@techmuffins.com";
    $headers = "From:" . $from;
    mail($to, $subject, $message, $headers );
}
?>

HTML:

<form id="contactform" action="" method="post" autocomplete="off">
    <input type="text" name="name" placeholder="name" autofocus required>
    <input type="email" id="email" name="email" placeholder="email" required>
    <textarea name="message" placeholder="message" required></textarea>
    <input type="submit" value="submit">
</form>
4

2 回答 2

0

为了在不刷新的情况下执行此操作,您通常会通过 Ajax 处理此问题。为此存在javascript函数。这是一个示例(使用 jQuery):

var frm = $(document.myform);
var data = JSON.stringify(frm.serializeArray());
于 2013-03-22T01:00:12.890 回答
0

将“your.php”更改为您的

  <script type="text/javascript">
            $(document).ready(function(){
                $("#contactform").submit( function () {    
                  $.post(
                   'your.php', 
                    $(this).serialize(),
                    function(data){
                      //if you have a div to show result (with id=results)
                      $("#results").html(data)
                   //if sucess hide the form as $("#contactform").hide()
                  // or .fadeOut() 
                    }
                  );
                  return false;   
                });   
            });
    </script>
于 2013-03-22T01:07:08.400 回答