我正在尝试将表单数据提交到另一个页面进行处理,并且当前需要接收任何数据。以下是有问题的表格。默认形式是用于请求用户名/密码的登录名。一个提交按钮。

                <div id="form_wrapper" class="form_wrapper">
                <form class="register">
                    <h3>Register</h3>
                    <div>
                        <label>Username:</label>
                        <input type="text" name="regname"/>
                        <span class="error">This is an error</span>
                    </div>
                    <div>
                        <label>Password:</label>
                        <input type="password" name="regpass" />
                        <span class="error">This is an error</span>
                    </div>
                    <div class="bottom">
                        <div class="remember"><input type="checkbox" /><span>Keep me logged in</span></div>
                        <input type="submit" value="Register"></input>
                        <a href="index.html" rel="login" class="linkform">You have an account already? Log in here</a>
                        <div class="clear"></div>
                    </div>
                </form>
                <form class="login active">
                    <h3>Login</h3>
                    <div>
                        <label>Username:</label>
                        <input type="text" name="username"/>
                        <span class="error">This is an error</span>
                    </div>
                    <div>
                        <label>Password: <a href="index.php" rel="forgot_password" class="forgot linkform">Forgot your password?</a></label>
                        <input type="password" name="password" />
                        <span class="error">This is an error</span>
                    </div>
                    <div class="bottom">
                        <div class="remember"><input type="checkbox" /><span>Keep me logged in</span></div>
                        <input type="submit" value="Login"></input>
                        <a href="index.php" rel="register" class="linkform">You don't have an account yet? Register here</a>
                        <div class="clear"></div>
                    </div>
                </form>
                <form class="forgot_password">
                    <h3>Forgot Password</h3>
                    <div>
                        <label>Username or Email:</label>
                        <input type="text" name="forgotuser" />
                        <span class="error">This is an error</span>
                    </div>
                    <div class="bottom">
                        <input type="submit" value="Send reminder"></input>
                        <a href="index.php" rel="login" class="linkform">Suddenly remebered? Log in here</a>
                        <a href="index.php" rel="register" class="linkform">You don't have an account? Register here</a>
                        <div class="clear"></div>
                    </div>
                </form>
            </div>


我想提交当前表格数据是什么

                $form_wrapper.find('input[type="submit"]')
                         .click(function(e){
                            e.preventDefault();
                              $.ajax({
                                    url: 'locallogin.php',
                                    type: 'POST',
                                    data: $(this).serialize(),
                                    success: function (results) {
                                      alert(results);
                                    }
                                    });
                         });


locallogin.php

<?php
print_r($_POST);
?>


现在唯一的响应是一个空数组。有任何想法吗?

最佳答案

如前所述,修复$(this)并将其设置为$("#form_wrapper")还将$POST修复为$_POST。您的JS应该看起来像这样。

$('#form_wrapper').find('input[type="submit"]')
.click(function(e){
    e.preventDefault();
    $.ajax({
        url: 'locallogin.php',
        type: 'POST',
        data: $(this).closest('form').serialize()+'&buttonName='+$(this).val(),
        success: function (results) {
            alert(results);
        }
    });
});


关于按钮名称的问题之后。

向ajax调用的数据行添加了代码。

关于javascript - AJAX/PHP多种形式的POST提交,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22433411/

10-15 03:04
查看更多