简体   繁体   English

PHP从不接收嵌套的jQuery $ .post数据

[英]PHP never receives nested jQuery $.post data

I'm trying to send simple form data from my index page to a PHP function, checklogin.php. 我正在尝试将简单的表单数据从索引页发送到PHP函数checklogin.php。 What I'm finding is that no data, serialized or even direct input, is getting through to the PHP function. 我发现,没有数据(无论是序列化的还是直接输入的)都无法传递到PHP函数。 I know for a fact that the jQuery function is getting called, and I also know that the PHP file is called because I've put various post functions in. 我知道一个事实,就是调用了jQuery函数,而且我也知道调用了PHP文件,因为我放入了各种post函数。

When I submit data directly from the form using action="checklogin.php" the $_POST data is correctly received and I can process it. 当我使用action="checklogin.php"直接从表单提交数据时,$ action="checklogin.php"数据已正确接收,我可以对其进行处理。 However when using jQuery to intercept the form submit, no data is received. 但是,当使用jQuery拦截表单提交时,不会接收到任何数据。

One thing to note is that the actual login form HTML is inserted from another page, to allow me to work on multiple pages instead of one big index file. 需要注意的一件事是,实际的登录表单HTML是从另一页插入的,这使我可以在多个页面上工作,而不是一个大索引文件。

Form - login.php 表格-login.php

<form id="loginform" name="loginform" method="post">
    <td>
        <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
            <tr>
                <td colspan="3"><strong>Member Login</strong></td>
            </tr>
            <tr>
                <td width="78">Username</td>
                <td width="6">:</td>
                <td width="294"><input name="username" type="text" id="username"></td>
            </tr>
            <tr>
                <td>Password</td>
                <td>:</td>
                <td><input name="password" type="text" id="password"></td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td><input type="submit" name="Submit" value="Login"></td>
            </tr>
        </table>
   </td>
</form>

Submit event hook - Index.php 提交事件挂钩-Index.php

$( document ).ready(function() {                
    $.post("login.php", function(data) { // retrieve login form from other page.
        $("#page5").html(data); 
        $('#loginform').submit(function () {
            event.preventDefault();
            var formData = $(this).serialize();

            console.log("Form Data: " + formData); // this is not blank

            $.post("checklogin.php", formData, function(data) {
                // post-login actions
            })

            });
        });
    })
});

checklogin.php (temporary until is problem resolved) checklogin.php (直到问题解决为止是临时的)

<?php
    print_r($_POST);
?>

As I said, even direct input such as "username=test$password=blah" instead of 'formData' results in an empty $_POST array. 如我所说,即使直接输入,例如“ username = test $ password = blah”,而不是“ formData”,也会导致一个空的$ _POST数组。 I've also tried using the ajax equivalent post call with no luck. 我也尝试过使用ajax等效的post调用,但是没有运气。

JavaScript Console Output JavaScript控制台输出

// console.log line above $.post
Form Data: username=test&password=blah

// Result from PHP print_r
Array
(
)

Use .on() 使用.on()

As elements are added dynamically you can not bind events directly to them .So you have to use Event Delegation . 由于元素是动态添加的,因此无法将事件直接绑定到它们。因此,必须使用事件委托

$(document).on('submit','#loginform',function () { //code here }

or better use 或更好地使用

$('#page5').on('submit','#loginform',function () { //code here }

Syntax 句法

$( elements ).on( events, selector, data, handler );

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

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