Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想在使用jQuery(或ajax)单击图像时运行PHP代码_Php_Facebook - Fatal编程技术网

我想在使用jQuery(或ajax)单击图像时运行PHP代码

我想在使用jQuery(或ajax)单击图像时运行PHP代码,php,facebook,Php,Facebook,我有以下代码来显示一个对话框时,图像被点击。我想运行PHP代码,而不是运行FB.ui。这是facebook的 <html> <head> <style> img#share_button { cursor: pointer; } </style> </head> <body> <div id="fb-root"></div> <script> window.fbAsyncIni

我有以下代码来显示一个对话框时,图像被点击。我想运行PHP代码,而不是运行FB.ui。这是facebook的

<html>
<head>
    <style> img#share_button { cursor: pointer; } </style>
</head>
<body>

<div id="fb-root"></div>

<script>
window.fbAsyncInit = function() {
FB.init({
    appId  : 'xxx',
    status : true, // check login status
    cookie : true, // enable cookies to allow the server to access the session
    xfbml  : true  // parse XFBML
});
};

(function() {
    var e = document.createElement('script');
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
}());
</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
这是图像:

<img id = "share_button" src = "img.png">
这是我需要更改的代码:

<script type="text/javascript">
$(document).ready(function(){
    $('#share_button').live('click', function(e){
        e.preventDefault();
        FB.ui({
            method: 'feed',
            name: 'TabPress1',
            link: 'http://www.hyperarts.com/',
            picture: 'http://www.hyperarts.com/',
            caption: 'I am a fan of TabPress',
            description: 'TabPress -- Gotta love it!',
            message: ''
        });
    });
});
</script>

</body>
</html>

我不认识任何JS,希望你能帮助我

为什么不使用href属性删除链接的底层和颜色并启动php脚本?

单击按钮时运行javascript函数:

<img id = "share_button" src = "img.png" onclick = "dothis()">
下面是一些基本的javascript:

<script type = "text/javascript">
function dothis(){
alert("Button pressed");
}
</script>
这只是一些基本的javascript,当点击按钮时会在屏幕上显示一条消息

编辑:看起来您想要使用JSON。看看这个:

因为jQuery已经存在:发送AJAX请求:

<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){
    $('#share_button').live('click', function(e) {
        e.preventDefault();
        $.ajax('/path/to/your/script.php');
    });
});
//]]>
</script>
有关更多信息,请参阅jQuery文档:


此外,我还添加了CDATA标记,以避免HTML特殊字符出现问题。这些特殊字符通常必须进行编码。

FB.uiparam1,param2方法可以采用两个参数。您已经指定了第一个用于指示提要对话框的显示方式。Param2可以是回调函数

FB.ui(
  {
    method: 'feed',
    name: 'Facebook Dialogs',
    link: 'http://developers.facebook.com/docs/reference/dialogs/',
    picture: 'http://fbrell.com/f8.jpg',
    caption: 'Reference Documentation',
    description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
  },
  function(response) {
    if (response && response.post_id) {
      alert('Post was published.');
    } else {
      alert('Post was not published.');
    }
  }
);

在发布post的代码开关中,您可以使用jQuery对一个PHP页面进行AJAX调用。请参阅:

如果您不懂任何javascript,也许最好查看一些初学者教程,如上的教程,但关于您的问题

看起来您正在使用jQuery。实现您所描述的功能的最好方法是使用AJAX,而jQuery在这方面有很好的功能

要根据jQuery中的元素ID从DOM中选择元素,只需执行以下操作:

$("#TheIdOfYourImage")
现在,听一下它被点击的时间

$("#TheIdOfYourImage").click(function(){
   //DO SOMETHING
}
现在,为了AJAX的乐趣。您可以在上阅读文档以了解更多技术细节,但归根结底就是这样

        $("#TheIdOfYourImage").click(function(){
            $.ajax({
              type: "POST",                                 // If you want to send information to the PHP file your calling, do you want it to be POST or GET. Just get rid of this if your not sending data to the file
              url: "some.php",                             // The location of the PHP file your calling
              data: "name=John&location=Boston",           // The information your passing in the variable1=value1&variable2=value2 pattern
              success: function(result){ alert(result) }   // When you get the information, what to do with it. In this case, an alert
            });
        }

根据你的评论,我认为你想做这样的事情:

$(document).ready(function(){
    // on click
    $('#share_button').live('click', function(e){
        e.preventDefault();

        // any parameters you need to pass to your php script, 
        // you can omit the data parameter if you don't need it
        var data = { param1: "a", param2: 2 }; 

        // start the ajax request
        $.post("your/script.php", data, function() {

            // when the ajax request completes, show the FB dialog
            FB.ui({
                method: 'feed',
                name: 'TabPress1',
                link: 'http://www.hyperarts.com/',
                picture: 'http://www.hyperarts.com/',
                caption: 'I am a fan of TabPress',
                description: 'TabPress -- Gotta love it!',
                message: ''
            });
        });
    });
});
相关javascript参考:


您需要PHP脚本的结果吗?PHP和javascript是两种不同的东西,运行方式也不同。Javascript在客户端,php在服务器上side@AlexandruRosianu好的,看看我的答案。FB.ui代码对你有用吗?为什么要用php替换它?@jrummell当用户单击图像时,我需要在显示对话框之前执行一些检查。如果这是您的意思,我不想在新窗口中启动它。这些语言有两个不同的用途。PHP是一种服务器端语言,用于后端功能,而JS是一种客户端语言,用于用户直接注意到的前端内容。也许您可以尝试使用JSON作为AJAX请求的一种形式?我希望PHP代码决定是显示对话框还是不显示对话框。实际上,我需要一个结果,我将使用Joseph Szymborski的代码