I've been at this for about 3 hours trying tons of different methods but I'm obviously doing something wrong and just can't seem to get it.
I have a php file that is generating a url from another php script in the variable $tweeturl:
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;
?>
This is tested and working fine.
I'm trying get the variable into JQuery via AJAX to populate the url for a blank browser window (I'm using wordpress which is why the function is wrapped) :
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>
I know i'm doing it wrong, but I just can't figure out what it is that i'm doing wrong.
Any help would be greatly appreciated.
EDIT TO SHOW THE WORKING SCRIPTS AFTER THE HELP RECEIVED HERE
Although all of the answers were correct, a lot went overboard with the JSON conversion that I don't require here. I really appreciate it though as i've learned more than I expected from this. But if like me, you simply want to grab a single var from your php script this is how it should be done...
tweet.php (same as original question but with the following echo added at the end)
echo $tweeturl;
In my naivety I just expected JQuery to pick up on the $tweeturl var without actually having to echo it. doh!
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>
Thanks again for all of your help guys, not just in helping me to get this working but by explaining some things that I didn't understand about JQuery.