Trigger PHP on simple Jquery event with Ajax

ajax, jquery, php

Solution

The posting data should be formatted as a JSON object.You need to format data to post like this,

var list = $(this).attr('id');
      $.ajax({
        url: '/update/apps/music.php',
          data :{playlist: list},
        type : 'POST',
        success : function(data) {
          $(".PlaylistHolder").load('/update/apps/music.php');
        }
      });

Or else you can post a serialized form like this

data :$('#yourFormId').Serialize(),

See Example Here

Problem

Based the ID of div clicked I want to be able to change an embed link. This code previously worked when I set up the page with a form. ``` <embed src= "/<?php if (isset($_POST['playlist'])) { // if ANY of the divs were clicked echo $_POST['playlist']; // echo the choice }else{ echo "5125962"; // As a default playlist } ?>"> ``` However my client disliked the page refreshing. Building on that, I discard the forms for this JavaScript ``` $(document).ready(function() { $(".playlist").click(function(){ var playlist = $(this).attr('id'); $.ajax({ url: '/update/apps/music.php', data : playlist, type : 'POST', success : function(data) { $(".PlaylistHolder").load('/update/apps/music.php'); } }); }); }); ``` in place of the .ajax function I also tried ``` $(".PlaylistHolder").load('/update/apps/music.php', playlist); ``` The idea was that in place of the whole page refreshing, it would reload the contents of the "PlayListHolder" div. I'm not getting any syntax errors, but my variable won't carry across. In my PHP I tried $_GET and $_REQUEST as well. Any ideas? Normally I fix my code on my own, but given the nature of debugging I think I'm going to throw up if I the default song one more time. Thanks guys

Original source