S. N
S. N

Reputation: 3949

get a JS value in php

I have a js function to return a title of a song and another one to return the artist of the same song. then I have another function which makes a string combining the name and the artist. finally I want to print that string in php. this is my js:

function getName(){

      return artist+ ", " + title;
  } 

and this is my php:

echo '<script>getName();</script>';

but this doesnt return anything. what am I missing??

Upvotes: 0

Views: 87

Answers (3)

andy
andy

Reputation: 2399

There's not a plausible way of doing this on a standard configuration. Javascript is evaluated after the PHP is. The PHP executes, then the server sends the results to the clients browser. The browser then renders that result, and THEN executes javascript. May I ask why you are doing this? It seems overly complicated. Why not just: document.write(getName());?

Upvotes: 1

GitaarLAB
GitaarLAB

Reputation: 14645

You must still output the return value:
echo '<script>document.write(getName());</script>'; does not need jQuery, but is simple vanilla javascript.

Upvotes: 5

Sherlock
Sherlock

Reputation: 7597

You're not putting it on screen.

alert(getName()); would work. Same goes for $('.element').text(getName()); or something similar.

Upvotes: 2

Related Questions