繁体   English   中英

联系表单不会发送数据

[英]Contact form doesn't send data

我在contact.php上有一个简单的联系表单,但提交它没有任何数据到相应的php文件(mail.php)

在contact.php里面形成

<form id="contact" action="" method="post">
<input id="name" type="text" name="name" width="250" size="35" placeholder="Your name">
<br><br>
<input id="email" type="text" name="email" width="250" size="35" placeholder="Your mail">
<br><br>
<textarea id="message" name="message" rows="6" cols="40" placeholder="Your message"></textarea>
<br><br>
<input type="button" value=" SEND " id="submit" />
<input type="reset" value="Reset" name="reset">
</form> 

js里面的contact.php

$('#submit').click(function(){
$.post("mail.php", $("#contact").serialize(), function(response) {
$('#success').html(response);
});
return false;
});  

mail.php

   print_r( $_POST );
    if((empty($_POST['name'])) || (empty($_POST['email'])) || (empty($_POST['message']))){
    die("All fields must be filled !");
    } 

结果:
Array ( ) All fields must be filled !

工作实例:

    <html>
        <head>
           <title>Example</title>
        <style type="text/css">
        div { color:blue; margin:3px; cursor:pointer; }
        </style>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
         <script type="text/javascript">
         $(document).ready(function() {
        $('#submit').click(function(e){
        $.post("mail.php", $("#contact").serialize(), function(response) {
        $('#success').html(response);
        });
        return false;
        });  
        }); 
        </script>
        </head>
        <body>
        <div id="success"></div>
            <form name="contact" id="contact" method="post">
            <?php
              for($i=1;$i<=10;$i++)
              {?>
              Text Box <?php echo $i; ?>&nbsp;<input type="text" name="in<?php echo $i; ?>"/><br/><br/>
              <?php
              }
            ?>
            <input type="submit" name="submit" id="submit"/>
            </form>

        </body>
    </html>

mail.php

<?php
    print_r( $_POST );
    if((empty($_POST['name'])) || (empty($_POST['email'])) || (empty($_POST['message']))){
    die("All fields must be filled !");
    } 
    ?>

这对我有用。

这是a.html文件

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
        <script type="text/javascript">
        $(function(){
            $('#submit').click(function(){
                $.post("x.php", $("#contact").serialize(), function(response) {
                    $('#success').html(response);
                });

            });  
        });

        </script>
    </head>
    <body>
        <form id="contact" action="" method="post">
            <input id="name" type="text" name="name" width="250" size="35" placeholder="Your name">
            <br><br>
            <input id="email" type="text" name="email" width="250" size="35" placeholder="Your mail">
            <br><br>
            <textarea id="message" name="message" rows="6" cols="40" placeholder="Your message"></textarea>
            <br><br>
            <input type="button" value=" SEND " id="submit" />
            <input type="reset" value="Reset" name="reset">
        </form> 
    </body>
    </html>

这是x.php文件

    <?php
       print_r( $_POST );
        if((empty($_POST['name'])) || (empty($_POST['email'])) || (empty($_POST['message']))){
        die("All fields must be filled !");
        } 
    ?>

试试吧,会阻止你的表单自然提交:

$('#submit').click(function(e){
    e.preventDefault();
});

您可能尝试的另一个变化是:

$("#contact").serializeArray()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM