1

I've been working with the Soundcloud API and have set up their record button to work on my site. The code spits out the new URL of the freshly created soundcloud file once the upload is done.

What I need is to send that URL through some PHP code which allows me to post a new WordPress post automatically.

The piece of javascript code comes from this soundcloud example (more code at the link): http://connect.soundcloud.com/examples/recording.html

<script type="text/javascript">

$("#upload").live("click", function(e){
  setRecorderUIState("uploading");

  SC.connect({
    connected: function(){
      $("#uploadStatus").html("Uploading...");
      SC.recordUpload({
        track: {
          title: "Untitled Recording",
          sharing: "private"
        }
      }, function(track){
        $("#uploadStatus").html("<a href='" + track.permalink_url + "' class='sclink'>" + track.permalink_url + "</a>");
      });
    }
  });

  e.preventDefault();
});

</script>

The #uploadStatus area is what spits out the track.permalink_url ... and it works like this. What I need though is to send the URL of the track to a php file which will auto-create a new WordPress post, like below:

<?php
// Create post object
$my_post = array(
 'post_title' => 'My Title',
 'post_content' => 'TRACKURLSHOULDSHOWUPHERE',
 'post_status' => 'publish'
);

// Insert the post into the database
wp_insert_post( $my_post );
?>

I know I need to utilize ajax but for the life of me I cannot figure out how to pull it properly.

Any help would be greatly appreciated.

maček
  • 76,434
  • 37
  • 167
  • 198
Mike
  • 17
  • 5
  • 1
    Post it with ajax, use the post in php. Read up on jQuery's ajax function, it's incredibly easy to use. – John V. Sep 21 '12 at 03:13
  • There's an error with your JavaScript paste; there's an extra `});` after `e.preventDefault();` and `e` is never even defined. – maček Sep 21 '12 at 03:15
  • @macek I just fixed that, I missed two lines of code at the top of the javascript. – Mike Sep 21 '12 at 03:19
  • possible duplicate of [Creating jQuery AJAX requests to a PHP function](http://stackoverflow.com/questions/7016701/creating-jquery-ajax-requests-to-a-php-function) and a million other questions. Please search the site (or Google) for "jQuery AJAX PHP" – user229044 Sep 21 '12 at 03:24
  • what is this `SC` you're using? – maček Sep 21 '12 at 03:25
  • @macek It's Soundcloud.com (I updated the post with the link to the record button codes they have up for use) – Mike Sep 21 '12 at 03:26

1 Answers1

0

http://api.jquery.com/jQuery.ajax/#sending-data-to-server

With the jQuery AJAX function, you could do this:

$.ajax({
  url: "myPhpScript.php",
  data: {
    url: track.permalink_url,
    name: track.name
  }
});

On the PHP end, you can retrieve these variables from the url (they are passed as GET by default, but you can change to POST if you like).

netpoetica
  • 3,375
  • 4
  • 27
  • 37
  • I have the php codes that insert a new WordPress post in a separate .php file. Getting the track.permalink_url output from the javascript to send to that .php file is where I'm stuck. I've been reading/editing codes for a couple days so it might just be that I'm a bit burnt out and missing something simple? – Mike Sep 21 '12 at 03:33
  • Maybe you could put the JavaScript function on a form Submit event with an action attribute to that particular PHP file with GET method. Put the data from the JavaScript into the attribute of the form element. For example, it starts off
    but before submit, target the 'action' attribute and add 'next.php&trackUrl=/escapedUrl%YadiYadi'
    – netpoetica Sep 21 '12 at 03:57
  • thanks, I need the code to automatically send to the php file without the user knowing (or having to click anything extra). The javascript spits out the URL of the soundcloud link once it's uploaded, so I'm trying to figure out how to, at that moment, send the URL to the php file. – Mike Sep 21 '12 at 04:02
  • Thanks Keith for posting the code. I'm having trouble figuring out where to put this I've tried adding it to a couple spots but it's not adding new posts so I'm assuming I have it added to the wrong spot in the javascript. – Mike Sep 21 '12 at 04:55
  • Thanks Keith, I went back and relooked at my code and realized I had the URL wrong so it wasn't working for me. Fixed the URL and it worked, I appreciate it. – Mike Sep 22 '12 at 03:36