简体   繁体   English

Facebook js SDK与Ajax变量一起发送到PHP

[英]Facebook js SDK send with ajax variabiles to PHP

First of all thank you for your atention. 首先感谢您的关注。 What I would like is to use the Facebook js SDK to get the user's name and id and send them to php in the same page (index.php). 我想要使​​用Facebook js SDK来获取用户的名称和ID,并将其发送到同一页面(index.php)中的php。 I manage to get the username and id and save them in 2 variables in JS (js_fb_id and js_fb_name), but when I try to make the ajax call and sent them to php, nothing happens. 我设法获取用户名和id并将它们保存在JS中的2个变量中(js_fb_id和js_fb_name),但是当我尝试进行ajax调用并将其发送到php时,什么也没发生。

So, on my index.php i have the ajax script 所以,在我的index.php上,我有ajax脚本

            var js_fb_id = response.id; 
            var js_fb_name = response.name; //this variables are set after the user is loged in
                $.post( 'index.php', 
                    {
                    php_fb_id: js_fb_id,
                    php_fb_id: js_fb_name
                    },

                function (data){

                     console.log(data);
                }
            ) 

Later on, I want to get the variables send via AJAX 稍后,我想获取通过AJAX发送的变量

        <?php if ( isset( $_POST['php_fb_id'] ) ) {
                echo $_POST['php_fb_id'];
            }
        ?>

Is this possible? 这可能吗? It works when I use a separate page to make the AJAX call, but I cannot get the php variables from there to my index.php page. 当我使用单独的页面进行AJAX调用时,它可以工作,但是无法从那里将php变量获取到我的index.php页面。

Thank you 谢谢

So basically what is happening here is that the ajax script is triggered on page load. 因此,基本上,这里发生的是ajax脚本在页面加载时触发。 However this is only after the dom has already been rendered. 但是,这仅在dom已经渲染之后。 To fix this you will need to trigger the ajax function on a specific event , for eg: button click. 要解决此问题,您需要在特定事件上触发ajax函数,例如:单击按钮。

Also note that since your entire page is in html you ajax function is returning the entire page as 'data' and not spacifically the php variables that you are trying to set.(You will be able to see this in the console) 还要注意,由于您的整个页面都是html格式,所以ajax函数会将整个页面作为``数据''返回,而不是要尝试设置的php变量(在控制台中可以看到)。

Also note that if you want to print the values on the page you will have to modify the html in the callback function through ajax for eg: 还要注意,如果要在页面上打印值,则必须通过ajax修改回调函数中的html,例如:

$(document).ready(function() {

 $('#post').click(function() {

      $.post( 'text.php', 
                    {
                    php_fb_id: js_fb_id,
                    php_fb_name: js_fb_name
                    },

                function (data, status){

                 $("#txt").html(data); //you can print returned data 
                     console.log(data);
                }
            ) 

  });
}); 

This should go in a separate php file say text.php 这应该放在一个单独的php文件中,例如text.php

<?php 
    if ( isset( $_POST['php_fb_id'] ) ) {
                echo $id = $_POST['php_fb_id'];
            }

?>

Your code looks fine, except that you passing php_fb_id twice in the parameter. 您的代码看起来不错,只不过您在参数中两次传递了php_fb_id。

$.post( 'index.php', {
   php_fb_id: js_fb_id,
  php_fb_id: js_fb_name
},

Except this, parameter are passing into index.php and getting captured too. 除此之外,参数也传递到index.php中并被捕获。 so you can go ahead with php operation there. 因此您可以在那里进行php操作。 Do let me know what is the problem you facing in this code? 让我知道您在此代码中面临的问题吗? will definitely try to help 一定会尝试帮助

Do Something like this: 做这样的事情:

if ($_SERVER['REQUEST_METHOD'] === 'POST') { //check if it's Post request
    if( isset( $_POST['php_fb_id']  && isset( $_POST['php_fb_name']) {
        $name = $_POST['php_fb_name'];
        $id = $_POST['php_fb_id'];
        ....
        echo "OK";
    }else{
        echo "ERROR, need input";
    }
else{
    //Your Index Code Here
}

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

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