Enjoy an ad free experience by logging in. Not a member yet?
Register .
06-01-2007, 03:58 PM
PM User |
#1
New Coder
Join Date: May 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
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!
06-01-2007, 04:06 PM
PM User |
#2
New Coder
Join Date: Jun 2007
Location: VA
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
GCharb
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
06-01-2007, 04:14 PM
PM User |
#3
New Coder
Join Date: May 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
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!
06-01-2007, 04:19 PM
PM User |
#4
New Coder
Join Date: Jun 2007
Location: VA
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
why are you echoing the $tmpVar afterwards?
06-01-2007, 04:21 PM
PM User |
#5
New Coder
Join Date: May 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
just a bad construct, I could write echo ?>" + url + "<?;
GCharb
__________________
If you have nothing good to say about someone, say nothing at all!
06-01-2007, 04:25 PM
PM User |
#6
New Coder
Join Date: Jun 2007
Location: VA
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Ok, last effort, Try this:
Code:
"<?
$tmpVar = "?>" + url + "<?";
06-01-2007, 04:41 PM
PM User |
#7
New Coder
Join Date: May 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
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!
06-01-2007, 04:48 PM
PM User |
#8
New Coder
Join Date: Jun 2007
Location: VA
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
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?
06-01-2007, 04:53 PM
PM User |
#9
New Coder
Join Date: May 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
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!
06-01-2007, 05:14 PM
PM User |
#10
New Coder
Join Date: Jun 2007
Location: VA
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
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.
06-01-2007, 05:25 PM
PM User |
#11
Banned
Join Date: Apr 2007
Posts: 428
Thanks: 29
Thanked 5 Times in 5 Posts
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
06-01-2007, 05:28 PM
PM User |
#12
New Coder
Join Date: Jun 2007
Location: VA
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by
matak
EDIT: ddanatzko, beat me to it
At least now I feel like I know what I'm talking about!!!
06-01-2007, 05:31 PM
PM User |
#13
Banned
Join Date: Apr 2007
Posts: 428
Thanks: 29
Thanked 5 Times in 5 Posts
Quote:
Originally Posted by
ddanatzko
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
)
06-14-2007, 03:35 PM
PM User |
#14
New Coder
Join Date: May 2007
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
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!
Jump To Top of Thread
Thread Tools
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
HTML code is Off
All times are GMT +1. The time now is 01:20 AM .
Advertisement
Log in to turn off these ads.