Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-01-2007, 03:58 PM   PM User | #1
GCharb
New Coder

 
Join Date: May 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
GCharb is an unknown quantity at this point
Passing jscript function variable to php

Hello all!

Little problem here!

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

Code:
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

Code:
function changeContent(url){

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

}
Anyone has an idea?

GCharb
__________________
If you have nothing good to say about someone, say nothing at all!
GCharb is offline   Reply With Quote
Old 06-01-2007, 04:06 PM   PM User | #2
ddanatzko
New Coder

 
Join Date: Jun 2007
Location: VA
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
ddanatzko is an unknown quantity at this point
Quote:
Originally Posted by GCharb View Post
Hello all!

Little problem here!

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

Code:
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:

Code:
function changeContent(url){

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

}
Dave
ddanatzko is offline   Reply With Quote
Old 06-01-2007, 04:14 PM   PM User | #3
GCharb
New Coder

 
Join Date: May 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
GCharb is an unknown quantity at this point
Hello there!

Thanks for the quick reply!

Tried and failed, same error message.

GCharb
__________________
If you have nothing good to say about someone, say nothing at all!
GCharb is offline   Reply With Quote
Old 06-01-2007, 04:19 PM   PM User | #4
ddanatzko
New Coder

 
Join Date: Jun 2007
Location: VA
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
ddanatzko is an unknown quantity at this point
why are you echoing the $tmpVar afterwards?
ddanatzko is offline   Reply With Quote
Old 06-01-2007, 04:21 PM   PM User | #5
GCharb
New Coder

 
Join Date: May 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
GCharb is an unknown quantity at this point
just a bad construct, I could write echo ?>" + url + "<?;

GCharb
__________________
If you have nothing good to say about someone, say nothing at all!
GCharb is offline   Reply With Quote
Old 06-01-2007, 04:25 PM   PM User | #6
ddanatzko
New Coder

 
Join Date: Jun 2007
Location: VA
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
ddanatzko is an unknown quantity at this point
Ok, last effort, Try this:

Code:
"<?
$tmpVar = "?>" + url + "<?";
ddanatzko is offline   Reply With Quote
Old 06-01-2007, 04:41 PM   PM User | #7
GCharb
New Coder

 
Join Date: May 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
GCharb is an unknown quantity at this point
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!
__________________
If you have nothing good to say about someone, say nothing at all!
GCharb is offline   Reply With Quote
Old 06-01-2007, 04:48 PM   PM User | #8
ddanatzko
New Coder

 
Join Date: Jun 2007
Location: VA
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
ddanatzko is an unknown quantity at this point
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?
ddanatzko is offline   Reply With Quote
Old 06-01-2007, 04:53 PM   PM User | #9
GCharb
New Coder

 
Join Date: May 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
GCharb is an unknown quantity at this point
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
__________________
If you have nothing good to say about someone, say nothing at all!
GCharb is offline   Reply With Quote
Old 06-01-2007, 05:14 PM   PM User | #10
ddanatzko
New Coder

 
Join Date: Jun 2007
Location: VA
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
ddanatzko is an unknown quantity at this point
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.
ddanatzko is offline   Reply With Quote
Old 06-01-2007, 05:25 PM   PM User | #11
matak
Banned

 
Join Date: Apr 2007
Posts: 428
Thanks: 29
Thanked 5 Times in 5 Posts
matak is on a distinguished road
Good luck with your try, hope u make it, but i think that is impossible.

It's the same as this code
PHP 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

EDIT: ddanatzko, beat me to it
matak is offline   Reply With Quote
Old 06-01-2007, 05:28 PM   PM User | #12
ddanatzko
New Coder

 
Join Date: Jun 2007
Location: VA
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
ddanatzko is an unknown quantity at this point
Quote:
Originally Posted by matak View Post
EDIT: ddanatzko, beat me to it
At least now I feel like I know what I'm talking about!!!
ddanatzko is offline   Reply With Quote
Old 06-01-2007, 05:31 PM   PM User | #13
matak
Banned

 
Join Date: Apr 2007
Posts: 428
Thanks: 29
Thanked 5 Times in 5 Posts
matak is on a distinguished road
Quote:
Originally Posted by ddanatzko View Post
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 )
matak is offline   Reply With Quote
Old 06-14-2007, 03:35 PM   PM User | #14
GCharb
New Coder

 
Join Date: May 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
GCharb is an unknown quantity at this point
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
__________________
If you have nothing good to say about someone, say nothing at all!
GCharb is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:20 AM.


Advertisement
Log in to turn off these ads.