User007
User007

Reputation: 41

Using $row['username'] within javascript

Is there a way to use $row['username'] within this script to have the actual signed in username showing up where 'USERNAME' is? (Assume I have a query set up that works, it's not shown).

<script src="http://connect.soundcloud.com/sdk.js"></script>
<div id="soundcloud"></div>
<script>
SC.oEmbed("https://soundcloud.com/USERNAME", {color: "434C58", maxheight: 350, maxwidth: 450},  document.getElementById("soundcloud"));
</script>

Thanks in advance, any help much appreciated!!

EDIT: this looks quite messy but it actually worked.

<script src="http://connect.soundcloud.com/sdk.js"></script>
<div id="soundcloud"></div>
<script>
SC.oEmbed("https://soundcloud.com/<?php
$sql = mysqli_query($con, "SELECT * FROM Users WHERE username = '" . $_GET["username"] . "'
");
while ($row = mysqli_fetch_array($sql)){
echo $row['soundcloud'] ;
    }  
?>", {color: "434C58", maxheight: 350, maxwidth: 450},  document.getElementById("soundcloud"));
</script>

Upvotes: 1

Views: 84

Answers (2)

Michelle
Michelle

Reputation: 31

You might want to place the embed URL within a link to the profile, and then draw the data out again when you need it.

This has the benefit of not being able to break the JavaScript in the event that the row data contains characters (such as newlines) which will cause a syntax error.

For example:

 <a id="scLink" href="https://soundcloud.com/<?php echo $row['username'];?>">

And then, in your JS:

 SC.oEmbed(
     document.getElementById('scLink').href,
     {},
     document.getElementById('soundcloud')
 );

Upvotes: 1

nullability
nullability

Reputation: 10675

Yes, simply echo the PHP variable inline:

<script src="http://connect.soundcloud.com/sdk.js"></script>
<div id="soundcloud"></div>
<script>
SC.oEmbed("https://soundcloud.com/<?php echo $row['username'];?>", {color: "434C58", maxheight: 350, maxwidth: 450},  document.getElementById("soundcloud"));
</script>

Upvotes: 2

Related Questions