【发布时间】: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 很陌生。