【问题标题】:Get PHP var from remote PHP script into JQuery从远程 PHP 脚本获取 PHP var 到 JQuery
【发布时间】:2014-12-17 12:23:36
【问题描述】:

我已经尝试了大约 3 个小时,尝试了很多不同的方法,但我显然做错了什么,似乎无法理解。

我有一个 php 文件,它从变量 $tweeturl 中的另一个 php 脚本生成 url:

tweet.php

<?php
$ch = curl_init("http://path/to/data.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$doit = curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($doit);
$show = $dom->getElementById("showmeta");
$liveshow = $show->nodeValue;
$dj = $dom->getElementById("djmeta");
$livedj = $dj->nodeValue;
$npshow = $liveshow;
$npshow = preg_replace('/\s+/', '+', $npshow);
$npdj = $livedj;
$npdj = preg_replace('/\s+/', '+', $npdj);

$url1 = "https://twitter.com/intent/tweet?source=webclient&text=Listening+to+";
$url2 = $npshow;
$url3 = $npdj;


$tweeturl .= $url1;
$tweeturl .= $url2;
$tweeturl .= $url3;
?>

这是经过测试并且工作正常。

我正在尝试通过 AJAX 将变量导入 JQuery 以填充空白浏览器窗口的 url(我正在使用 wordpress,这就是函数被包装的原因):

HTML

<a class="player-twitter" href="#"</a>

JQuery

<script type="text/javascript">
(function($) {
    $(document).ready(function() {
        $( "a.player-twitter" ).click(function( event ) {
            $.ajax({
                type: "GET",
                url: "http://path/to/tweet.php",
                data: { var: $tweeturl },
                success: function(e) {
                    window.open(<?php echo $tweeturl; ?>);
                }
            });
        })
    });
})( jQuery );
</script>

我知道我做错了,但我就是不知道我做错了什么。

任何帮助将不胜感激。

在此处收到帮助后编辑以显示工作脚本

尽管所有答案都是正确的,但我在这里不需要的 JSON 转换有很多过分的地方。我真的很感激,因为我从中学到的东西比我预期的要多。但是如果像我一样,你只是想从你的 php 脚本中获取一个 var,这就是应该这样做的......

tweet.php(与原始问题相同,但末尾添加了以下回声)

echo $tweeturl;

在我的天真中,我只是希望 JQuery 能够接收 $tweeturl 变量,而不必实际回显它。呵呵!

JQuery

<script type="text/javascript">
(function($) {$(document).ready(function() {
$( "a.player-twitter" ).click(function( event ) {
$.ajax({
type: "GET",
url: "http://path/to/tweet.php",
success: function ( response_string ) {
    window.open( response_string );
}
});
})
});})( jQuery );
</script>

再次感谢你们所有的帮助,不仅仅是帮助我完成这项工作,还解释了一些我对 JQuery 不了解的事情。

【问题讨论】:

  • 这个问题的代码是如此的不清楚和混乱。为什么你将$tweeturl 放在GET 参数中,但你不在PHP 中使用它?
  • @DanFromGermany 因为他显然对 javascript 和 PHP 很陌生。

标签: php jquery ajax get


【解决方案1】:

现在这里有一些使用 JSON 的答案。但这在您的示例中是不必要的,因为您只想返回一个字符串。所以这是一个简单的解决方案:

在您的 PHP 中,您必须输出您想要返回给您的 ajax 调用的内容。所以只需在 PHP 文件的末尾添加:

echo $tweeturl;


在您的 javascript 中更改您的 ajax 调用:

$.ajax({
    type: "GET",
    url: "http://path/to/tweet.php",
    success: function ( response_string ) {
        window.open( response_string );
    }
});

就是这样 - 当您不向 PHP 文件发送任何数据时,您甚至不需要 data:..。

【讨论】:

  • @northkildonian 虽然其他答案都有效,但就像你说的那样,他们不必要地将数据转换为 JSON,在这种情况下我不需要这样做。这个短小甜美,是一种享受。非常感谢您帮助我更好地了解 JQuery 如何与 PHP 交互。
【解决方案2】:

您只需要在您的 tweet.php 文件中回显$tweet,就在您生成它的最后。 一旦你从你的 ajax 调用 tweet.php,代码就会执行,并且 echo 会作为参数发送给你 ajax 成功。

代码:

tweet.php

<?php
$ch = curl_init("http://path/to/data.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$doit = curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($doit);
$show = $dom->getElementById("showmeta");
$liveshow = $show->nodeValue;
$dj = $dom->getElementById("djmeta");
$livedj = $dj->nodeValue;
$npshow = $liveshow;
$npshow = preg_replace('/\s+/', '+', $npshow);
$npdj = $livedj;
$npdj = preg_replace('/\s+/', '+', $npdj);

$url1 = "https://twitter.com/intent/tweet?source=webclient&text=Listening+to+";
$url2 = $npshow;
$url3 = $npdj;


$tweeturl .= $url1;
$tweeturl .= $url2;
$tweeturl .= $url3;
$url = array("url"=>$tweeturl);
//echo the tweet url back to your ajax
echo json_encode($url);
?>

ajax

<script type="text/javascript">
(function($) {$(document).ready(function() {
  $( "a.player-twitter" ).click(function( event ) {
    $.ajax({
      type: "GET",
      url: "http://path/to/tweet.php",
      success: function(tweeturl){
        /* "tweeturl" is the JSON object you receive from the tweet.php file */
        /* "url" is the name of your array key in the tweet.php file */
        var obj = jQuery.parseJSON(tweeturl);
        window.open(obj.url);
      }
    });
  })
});})( jQuery );
</script>

【讨论】:

  • 谢谢大家,我已经尝试了其中一些方法,但没有一个打开带有 url 的新窗口?对不起,我对 javascript 不太了解,这就是为什么我没有回应 $tweeturl 因为如果它留在 php 中我不需要这样做,我可以直接调用它。再次感谢您的所有帮助,我学习缓慢;)
【解决方案3】:

由于您在这段代码中没有使用任何参数,因此您只需在最后添加 echo 将生成的 tweeturl 传回即可。

<?php
$ch = curl_init("http://path/to/data.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$doit = curl_exec($ch);
$dom = new DOMDocument();
@$dom->loadHTML($doit);
$show = $dom->getElementById("showmeta");
$liveshow = $show->nodeValue;
$dj = $dom->getElementById("djmeta");
$livedj = $dj->nodeValue;
$npshow = $liveshow;
$npshow = preg_replace('/\s+/', '+', $npshow);
$npdj = $livedj;
$npdj = preg_replace('/\s+/', '+', $npdj);

$url1 = "https://twitter.com/intent/tweet?source=webclient&text=Listening+to+";
$url2 = $npshow;
$url3 = $npdj;


$tweeturl .= $url1;
$tweeturl .= $url2;
$tweeturl .= $url3;

// ADDED CODE TO RETURN THE RESULT OF ALL THIS PROCESSING
// but do it in a JSON way
$result = new stdClass();
$result->tweetUrl = $tweeturl;
echo json_encode($result);
?>

现在在 javascript 中,您可以通过 ajax 调用将数据作为可以转换为 json 对象的字符串返回给您

<script type="text/javascript">
(function($) {
    $(document).ready(function() {
        $( "a.player-twitter" ).click(function( event ) {
            $.ajax({
            type: "GET",
            url: "http://path/to/tweet.php",
            // this was not used in the PHP so dont need to pass it as a parameter
            //data: { var: $tweeturl },
            success: function(data){
               result = $parseJSON(data); // convert json string to json object
               window.open(result.tweetUrl);
               }
          });
      })
   });
})( jQuery );
</script>

【讨论】:

  • @DanFromGermany 好吧,我看不到它在他正在调用的 PHP 代码中使用,即tweet.php
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-26
  • 2011-04-15
  • 1970-01-01
  • 2015-07-08
  • 2013-06-03
  • 1970-01-01
相关资源
最近更新 更多