Hello,

I have a snippet of javascript which is pulling a cookie file and translating the variable "myuser" into the currently logged in value (shown below)...

var myuser = readcookie('user');

This is working successfully and I can write it to a page. But this isn't quite enough because I'm trying to compare that value (someone's username) to a MySQL database field which stores a picture number such as 45625.jpg and attempting to write that to a page so I can use a include file command at the end of an img src html command to display that particular users thumbnail picture. How I haven't lost you yet 😉

What I'm trying to do now is send this variable to a php query. I have already successfully sent it to a php variable, and can verify this by using echo with the newly created php variable, $newvar, which interpreted the javascript variable from document.write as myuser.

$newvar = "<script language=JavaScript> document.write(myuser);</script>";
$query = "SELECT picture FROM community_users WHERE user='$newvar'";
$result = mysql_query($query)
    or die("Query failed");

The problem is in the actual query... the $newvar never parses as anything. Yet if I write it out with an echo such as:

echo $newvar;

... it works fine.

What method do I have to use to have this variable be interpreted before php attempts to process on the server-side?

Thanks for any help!

    What you're trying is impossible 😉

    Think about it:
    Javascript is clientside - php is serverside.
    What you're getting with the echo'ing of your var is that the browser iterprets the javascript as the page loads. This is done by the browser, which executes the javascript in the client only.
    If you don't echo anything, the var will hold the original js as it's value.
    Even if you echo the var, then try to access the value as php below, the value will still hold the original javascript as it's value: it's the browser that executes the javascript, not the parser.

    The only way to send js-values to php is by forms or url-manipulation.

    I've tried this too, once 😃

    knutm

      Thanks for the reply.

      So, is there any way to pull a value from part of a cookie directly through PHP?

      Or, to end all, what method would I use to send the JS variable through a form, or at least making a hard copy of that so it may be plugged into a query via PHP?

      Thanks again... I'm a newbie obviously. 😉

        You can pull a cookie in PHP using the $COOKIE super-global, it works the same way as $GET and $_POST.

          That worked like a charm! I scrapped all instances of javascript and used this directly in the PHP and it worked absolutely perfect.

          Thank you!!!

            Write a Reply...