发表没有表格提交和GET

时间:2013-11-04 04:58:13

标签: php ajax

我正在重定向到新页面start.php并以这种方式传递变量:

window.location.href = 'start.php?&userid= + userid;`

我可以这样做:

$.post('start.php',{userid: userid});
window.location.href = 'start.php';

我不想使用GET和表单提交。

因为在同一页面上还有其他进程已经将数据发布到其他页面。

我在上面测试但是在start.php上它表示var未定义

更新

start.php

<?php
    $user_id=$_GET['userid']; //When I use GET
?>

<?php
    $user_id=$_POST['userid']; //When I use POST
?>

的login.php

<html>
    <head>
        <title>ThenWat</title>
        <link href="css/button.css" rel="stylesheet" type="text/css">
        <link href="css/rateit.css" rel="stylesheet" type="text/css">
        <script src="//connect.facebook.net/en_US/all.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script src="js/jquery.rateit.js" type="text/javascript"></script>          
        <style>
            .header{
                background-color:#0B6121;
                border:2px solid #0B6121;
                padding:10px 40px;
                border-radius:5px;
            }

            .middle{
                background-color:Yellow;
            }

            .left{
                background-color:Green;
            }
            .url{
                box-sizing: border-box;
                display: block;
            }
            .url:hover {
                box-shadow: 2px 2px 5px rgba(0,0,0,.2);
            }

            html, body { margin: 0; padding: 0; border: 0 }
        </style>
    </head>

    <body>
        <div class="header" style="">
            <table style="">
                <tr>
                    <td><img src= "3.png" height="50" width="310"/></td>
                </tr>
            </table>                    
        </div>      
            <table border="0" width="100%">
                <tr>
                    <div class="middle">
                        <td style="width:40%">
                        <input type="button" id="loginButton" class="button" onclick="authUser();" value="Login | ThanWat" style="display:none; left:500px; position:relative"/> 
                            <lable id="lable1"  style="display:none;" ><i> Please wait .. </i> </lable>                     
                        <div class="rateit bigstars" id="rateit99" data-rateit-starwidth="32" data-rateit-starheight="32" style=" position:relative; top:-30px; display:none; left:300px" >
                            </div>
                        </td>               
                    </div>
                </tr>               
            </table>
        <div id="fb-root"></div>
        <script type="text/javascript">
            var userid;
            FB.init({
            appId: '1412066',
            xfbml: true,
            status: true,
            cookie: true,
            });
            FB.getLoginStatus(checkLoginStatus);
            function authUser() 
            {
                FB.login(checkLoginStatus, {scope:'email'});
            }
            function checkLoginStatus(response) 
            {               
                document.getElementById('lable1').style.display = 'block';
                if(response && response.status == 'connected') 
                {
                FB.api('/me?fields=movies,email,name', function(mydata)
                {                   
                    console.log(mydata.email);
                    console.log(mydata.id);
                    userid=mydata.id;
                    var name=mydata.name;
                    //alert(name);
                    var email=mydata.email;
                    var json = JSON.stringify(mydata.movies.data);
                    var a = JSON.parse(json);
                    var picture="https://graph.facebook.com/"+userid+"/picture?type=small";
                    // alert(picture);
                    $.post('user_record.php',{'myd':a, name: name, email: email, userid:userid, picture:picture}, function(data)
                    {
                        window.location.href = 'start.php?userid='+userid;

                    });
                });

                console.log('Access Token: ' + response.authResponse.accessToken);
                }
                else
                {
                    document.getElementById('lable1').style.display = 'none';
                    document.getElementById('loginButton').style.display = 'block';
                }
            }
        </script>

    </body>

</html>

UPDATE2

$.post('user_record.php',{'myd':a, name: name, email: email, userid:userid, picture:picture}, function(data)
                    {                       
                        var $form = $("<form id='form1' method='post' action='start.php'></form>");
                        form.append('<input type="hidden" name="userid" value="'+userid+'" />');
                        $('body').append($form);
                        window.form1.submit();
                    });

start.php

<?php
    $user_id=$_POST['userid'];
    echo $user_id;
?>

1 个答案:

答案 0 :(得分:1)

这是一个适合我的解决方案。您需要在第一个ajax响应后使用jquery添加新表单,然后使用javascript提交此表单。

<script>

 $.post('user_record.php',{'myd':a, name: name, email: email, userid:userid, picture:picture}, function(data){
   var $form = $("<form id='form1' method='post' action='start.php'></form>");
   $form.append('<input type="hidden" name="userid" value="'+data+'" />');
   $('body').append($form);
   window.form1.submit();
 });

 </script>

请根据您的要求进行修改。希望这有帮助