0

I perform a mysql query SELECT * FROM table WHERE mp3link = '$songname' I want the variable $songname to = a jquery variable named 'key' which changes depending on the song playing. So far everything works when i set $songname ='illuminati.mp3' it will fetch and echo 'Illuminati' in the text. but when I try to exit the php and fetch the jquery variable it wont work. I was reading that PHP is server side and Jquery is client side so this may be why? but then I heard about a language called AJAX that may be able to do this?

 $(".blocksong").on('click', function () {
 var key = $(this).attr('key');
 EvalSound(this, key);
 var this_play = $(this);
 $(".blocksong").each(function () {
     if ($(this)[0] != this_play[0]) {
         $(this).removeClass("blockpause");
     }
 });
 $(this).toggleClass("blockpause");
 $(".nowplaying").text("<?php 
 $songname = " key "; // how do I insert var key here?
 $nowplay=mysql_fetch_assoc(mysql_query("SELECT * FROM `soundplum` WHERE mp3link = '$songname'"));
 echo $nowplay['name'];

 ?>");
Jeff
  • 1,018
  • 1
  • 15
  • 33
  • 1
    Ajax != language, it's is short for Asynchronous JavaScript and XML, more like a methodology. – Shehary Oct 04 '15 at 01:59
  • 1
    Hi Jeff. some clafications: PHP is a scripting-language usually run on a (web-) server. jQuery is only a *wrapper* of the scripting-language javascript, that usually runs in the browser (=client). AJAX ist a ...hm... method of how to use javascript (namly asynchronous. AJAX = Asynchronous javascript and XML). Please have further readings about this and your very welcome to come back if you experience problems! – Jeff Oct 04 '15 at 02:00
  • 1
    right now you're trying to execute php code (=server) in javascript (=client). that won't work – Jeff Oct 04 '15 at 02:01
  • 1
    and furthermore don't use *mysql_fetch_assoc* and things that start with *mysql_* (and don't have an i). It's deprecated and will be removed. User eighter *msqli* or PDO – Jeff Oct 04 '15 at 02:06

2 Answers2

1

After all my comments, to give you an answer to this, that hopefully will help you:

You want to execute a (clientside) javascript function that fetches data from your server/database. To do that you will need an AJAX function. An AJAX function makes a call to the server (in your case it would call a php script) and waits for an answer. As soon as it gets an answer it'll execute another function, that you specified. Have a look here: http://www.w3schools.com/ajax/

Since you already use jQuery also have a look at jQuery's version of the same: http://api.jquery.com/jquery.ajax/

So here's what you can do (jQuery-Version, a copy of the tuorial):

// in your html / script
// after $(this).toggleClass("blockpause");
$.ajax({
  url: "getsongname.php?songname=" + key,
  context: document.body
}).done(function(response) {
   $(".nowplaying").text(response);
});

where you'd have a php-script called getsongname.php that'd have something like this:

<?php
  //...something before that to establish db-connection and stuff...

  $songname = $_POST['songname'];

  // this is a copy of your code!! Please don't use msql_... anymore!!
  $nowplay=mysql_fetch_assoc(mysql_query("SELECT * FROM `soundplum` WHERE mp3link = '$songname'"));
  echo $nowplay['name'];
?>

Note, that 1. don't use mysql anymore. 2. what you echo in php here will be your response in the AJAx-call!

I hope this'll help.

Jeff
  • 6,895
  • 1
  • 15
  • 33
  • Thanks Jeff! I see now that AJAX is a technique for creating dynamic pages. I also found http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php Why you shouldn't use MySQL – Jeff Oct 04 '15 at 11:45
0

I just realized how I could do this, I added another attribute called name and fetched the name from above PHP... This isn't really the answer to the question of how to insert jquery into php, but it works. It seems like you gotta find a different way to do something if the first idea doesn't work, but usually there is a way. Thanks Shehary and Jeff for the information on AJAX methodology, I will also have to switch to the msqli or PDO eventually too!

PHP

while($sound=mysql_fetch_assoc($records)){
    echo "<div class='blocksong' name='".$sound["name"]."' key='".$sound["mp3link"]."'>".$sound['name']."</div>";

    }
?>

JQuery

$(".blocksong").on('click', function () {
     var key = $(this).attr('key');
     var name = $(this).attr('name');
     EvalSound(this, key);
     var this_play = $(this);
     $(".blocksong").each(function () {
         if ($(this)[0] != this_play[0]) {
             $(this).removeClass("blockpause");
         }
     });
     $(this).toggleClass("blockpause");
     $(".nowplaying").text(name);
 });
Jeff
  • 1,018
  • 1
  • 15
  • 33
  • that's a good solution! There's not really a need for an AJAX-call, since you already have the information you need when writing the html. Glad you came to that solution yourself! I anyway posted you an answer of how to do it via an ajax call. – Jeff Oct 04 '15 at 02:28