2012-07-01 110 views
0

我有一段代碼,我想在Facebook返回用戶詳細信息5秒後自動提交。但是代碼無法自動提交。我無法找到一個錯誤。 我的代碼如下所示Javascript未能自動提交表格

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

回答

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

我試過,但它仍然沒有提交。 – sammyukavi

+0

你確定登錄功能被觸發嗎? – Jashwant

+0

是的。嘗試一下代碼並看看。登錄獲取詳細信息並填充表單,但表單未提交。 – sammyukavi