...

View Full Version : Passing jscript function variable to php



GCharb
06-01-2007, 03:58 PM
Hello all!

Little problem here!

I am trying to pass a jscript function variable to php, here's an example.


function changeContent(url){

document.getElementById("contentDiv").innerHTML = "<?
$tmpVar = ?>" + url + "<?
echo $tmpVar;
?>";

}

This gives me an error "Parse error: parse error, unexpected ';' in /home/azranet/public_html/test.php on line 10"

On the other hand, this works


function changeContent(url){

document.getElementById("contentDiv").innerHTML = "<?
$tmpVar = "some text";
echo $tmpVar;
?>";

}

Anyone has an idea?

GCharb

ddanatzko
06-01-2007, 04:06 PM
Hello all!

Little problem here!

I am trying to pass a jscript function variable to php, here's an example.


function changeContent(url){

document.getElementById("contentDiv").innerHTML = "<?
$tmpVar = ?>" + url + "<?
echo $tmpVar;
?>";

}

This gives me an error "Parse error: parse error, unexpected ';' in /home/azranet/public_html/test.php on line 10"



Not sure if it will work but try this:


function changeContent(url){

document.getElementById("contentDiv").innerHTML = "<?
$tmpVar = ?>" + url + "<?;
echo $tmpVar;
?>";

}

Dave

GCharb
06-01-2007, 04:14 PM
Hello there!

Thanks for the quick reply!

Tried and failed, same error message.

GCharb

ddanatzko
06-01-2007, 04:19 PM
why are you echoing the $tmpVar afterwards?

GCharb
06-01-2007, 04:21 PM
just a bad construct, I could write echo ?>" + url + "<?;

GCharb

ddanatzko
06-01-2007, 04:25 PM
Ok, last effort, Try this:


"<?
$tmpVar = "?>" + url + "<?";

GCharb
06-01-2007, 04:41 PM
Hello again!

This time no errors but a value of zero(0) is assigned to the php variable, ill figure it out, thanks for your time! ;)

ddanatzko
06-01-2007, 04:48 PM
I'm still kinda confused as to what you're trying to do. From my understanding (keep in mind I've only been writing code for about 6 months now) Javascript is a Client Side scripting languange, whereas, PHP is server side. So, I don't really think you can assign PHP variables in a Javascript funtion. can you paste all of your php code up so I can take a look?

GCharb
06-01-2007, 04:53 PM
You are right about jscript being client side and php being server side, but, it is easy to assing php variable to jscript like so...

var myJsVar <?= $myPhpVar; ?>

Has to be a way to do the opposite and what I am tying to do here is to update the content of a html Div with php generated through jscript!

Hope it sheds light on my intentions! :)

GCharb

ddanatzko
06-01-2007, 05:14 PM
Since your browser doesn't actually parse php, the variable has to be set on the serverside for PHP, so if you're trying to assign a jscript variable to a php variable that hasn't been set (since the browser hasn't set it yet) you would get a value of 0. Sounds like you're going to need some other technology. AJAX might be the way to go.

matak
06-01-2007, 05:25 PM
Good luck with your try, hope u make it, but i think that is impossible.

It's the same as this code


<?php

$variable = ?>

<script type="text/javascript">
document.write("Hello World!") <?php ; ?>
</script> <?php

echo $variable;

?>

And that's just wrong, couse PHP server side parser, parses through that file before it reaches the user. Maybe you should use Ajax for that, although interesting idea. If you make it, post it back here, you'll be sort of a hero :rolleyes:

EDIT: ddanatzko, beat me to it :rolleyes:

ddanatzko
06-01-2007, 05:28 PM
EDIT: ddanatzko, beat me to it :rolleyes:

At least now I feel like I know what I'm talking about!!!;)

matak
06-01-2007, 05:31 PM
At least now I feel like I know what I'm talking about!!!;)

Don't let me be the measure of -- that you know what ur talking about :lol:

(that fraze looses some meaning due to translation :D)

GCharb
06-14-2007, 03:35 PM
Hello there!

Always feels good to have the impression we know what we are talking about, happens even to me from time to time! ;)

Solved the problem with ajax, loving it too

The httprequest aproach allows me to pass any jscript variable to php I need.

Thanks both for your time! :)

GCharb



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum