Javascript无法自动提交表单

时间:2012-07-01 14:45:37

标签: javascript facebook

我有一个代码,我想在Facebook返回用户详细信息后5秒自动提交。但是代码无法自动提交。我找不到错误。 我的代码如下所示

<?php 
   //some php functions to process the submitted form
 ?>       
        <div>
            <div id="fb-root"></div>        
            <script type="text/javascript">
                window.fbAsyncInit = function() {
                    FB.init({appId: 'xxxxxxxxxxxxxxxx', status: true, cookie: true, xfbml: true});
                    /* All the events registered */
                    FB.Event.subscribe('auth.login', function(response) {
                       login();
                    });
                    FB.Event.subscribe('auth.logout', function(response) {

                        logout();
                    });
                    FB.getLoginStatus(function(response) {
                        if (response.session) {
                            login();
                        }
                    });
                    FB.getLoginStatus(function(response) {
                        login();
                        logout();
                    });
                };
                (function() {
                    var e = document.createElement('script');
                    e.type = 'text/javascript';
                    e.src = document.location.protocol +
                        '//connect.facebook.net/en_US/all.js';
                    e.async = true;
                    document.getElementById('fb-root').appendChild(e);
                }());

                function login(){
                    FB.api('/me',function(response) {

                        if(typeof response.name != 'undefined')
                        {
                            document.getElementById('hideDiv').style.visibility = 'hidden';                      
                            document.signupForm.first_name.value=response.first_name;
                            document.signupForm.last_name.value=response.last_name;
                            document.signupForm.username.value=response.username;
                            document.signupForm.email.value=response.email;

                        }

                    });
                }

                function logout(){
                    //document.getElementById('login').style.display = "none";
                }

                setTimeout("document.signupForm.submit();",5000); //5 seconds

            </script>
           <form id="signupForm" name="signupForm" class="phorm2" action="" method="post">
                <h3>Sign up</h3>
                <div id="hideDiv">Get details from Facebook
                    <fb:login-button autologoutlink="true" perms="email,user_birthday,status_update,publish_stream"></fb:login-button>
                </div>
                <br>
                <label class="short" for="first_name">First Name</label>
                <input type="text" name="first_name" id="first_name" />
                <br>
                <label class="short" for="last_name">Last Name</label>
                <input type="text" name="last_name" id="last_name" />
                <br>
                <label class="short" for="username">Username</label>
                <input type="text" name="username" id="username" /> 
                <br>
                <label class="short" for="email">Email</label>
                <input type="text" name="email" id="email"/>
                <br>
                <label class="short" for="submit">&nbsp;</label>
                <input type="submit" name="submit" value="Sign me up" class="Cart66ButtonPrimary" />
            </form>
        </div>

1 个答案:

答案 0 :(得分:0)

你的代码在加载头后立即运行,那时没有任何形式。

从当前位置删除此行

setTimeout("document.signupForm.submit();",5000); //5 seconds

并在此处添加,

FB.api('/me',function(response) { 
    if(typeof response.name != 'undefined'){
        document.getElementById('hideDiv').style.visibility = 'hidden';                      
        document.signupForm.first_name.value=response.first_name;
        document.signupForm.last_name.value=response.last_name;
        document.signupForm.username.value=response.username;
        document.signupForm.email.value=response.email; 
    }
   setTimeout(function(){
       document.signupForm.submit();
   },5000); //5 seconds  
    // Never put a string in as parameter in  setTimeout
});