0

我有一个简单的联系表格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;
});  

邮件.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 !

4

3 回答 3

2

工作示例:

    <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>

邮件.php

<?php
    print_r( $_POST );
    if((empty($_POST['name'])) || (empty($_POST['email'])) || (empty($_POST['message']))){
    die("All fields must be filled !");
    } 
    ?>
于 2013-09-30T13:53:52.713 回答
1

这对我有用。

这是一个 .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 !");
        } 
    ?>
于 2013-09-30T13:58:55.927 回答
-1

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

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

您可以尝试的另一项更改是:

$("#contact").serializeArray()
于 2013-09-30T13:26:39.157 回答