简体   繁体   中英

Javascript fails to autosubmit form

I have a code that I want to auto-submit 5 seconds after Facebook has returned the user details. However the code fails to auto submit. I cant find an error. My code is shown below

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

Your code runs as soon as the head is loaded, there was no form at that time.

Remove this line from current position

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

And add here,

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
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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