2

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.

Grant
  • 1,297
  • 2
  • 16
  • 39

3 Answers3

0

You simply need to echo the $tweet in your tweet.php file, right at the end after you generate it. Once you call tweet.php from your ajax, the code will execute and the echo will be sent to you ajax success as a parameter to function.

code:

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>
Community
  • 1
  • 1
  • Thanks guys, I've tried a few of these methods and none are opening the new window with the url? I'm sorry I'm not very clued up about javascript, that's why I wasn't echoing the $tweeturl as I don't need to do that if it's staying in php, I can just call it directly. Again thanks for all of your help, i'm learning slowly ;) – Grant Dec 17 '14 at 14:40
0

As you are not using any parameters in this piece of code all you need to do is pass back the generated tweeturl by adding an echo at the end.

<?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);
?>

Now in the javascript you have data being returned to you from the ajax call as a string that can be converted to a json object so

<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>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0

there are some answers here right now which use JSON. but that's unnecessary in your example since you only want to return a string. so here is a simple solution:

in your PHP you have to output what you want to return to your ajax-call. so just add this at the end of your PHP file:

echo $tweeturl;


in your javascript change your ajax call:

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

that's it - you don't even need data:.. here when you don't send any data to your PHP file.

low_rents
  • 4,481
  • 3
  • 27
  • 55
  • @northkildonian Although the other answers all worked, like you say they were unnecessarily converting the data to JSON which I don't need to do in this instance. This one is short and sweet and works a treat. Many thanks for helping me understand how JQuery interacts with PHP a little better. – Grant Dec 17 '14 at 15:04