Passing values('sessions') from PHP to javascript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
victhour
Forum Commoner
Posts: 38
Joined: Fri Feb 17, 2006 12:59 am
Contact:

Passing values('sessions') from PHP to javascript

Post by victhour »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hello guys,

bellow is my PHP code that list all the data or articles from database which return a popup window('a Javascript driven')  [color=blue]URL[/color] of the selected article. now my problem is how to pass PHP values or sessions to Javascript ('or Vice Versa')? anyone care to help please!...

Code: Select all

<div class="lisTxt">
<?php echo $loc.' - '.$sec.': '; ?> <a href="view_article.php?article_id=<?= $ctr['mas_id']; ?>" target="_blank"><?= filval(charslen($ctr['title'])); ?></a>
</div>
any help is very much appreciated...

tanx in advanced...


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

You need to explain your code...

If I understood your code correctly, it opens in a blank window (not JS pop-up) some PHP page.

Basically, you don't need to pass the session. PHP stores cookies on the client's machine so every time the client opens new page, PHP knows automatically the session ID.

BUt, you also can pass the session like that:

Code: Select all

<?php
session_start();

//The rest of the code goes here...

echo "<a hred=\"view.php?var=1&".SID."\" title=\"\">Click Me</a>";

?>
SID will echo the current session ID so view.php will use that session.[/syntax]
wyrmmage
Forum Commoner
Posts: 56
Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID

Post by wyrmmage »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


right. If you need to pass values from php to javascript, you can do something like, say:

Code: Select all

<?php

$x = "something";
$y = "something";

echo('
<SCRIPT TYPE="text/javascript">
var x = ' . $x . ';
var y = ' . $y . ';
var z = x + y;
document.write(x + ", " + y + ", " + z + ".");
</SCRIPT>
');

?>
I'm pretty sure that the above code will work, but my web-server is down right now, so I can't test it, sorry :(


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
victhour
Forum Commoner
Posts: 38
Joined: Fri Feb 17, 2006 12:59 am
Contact:

Post by victhour »

Code: Select all

<script type="text/JavaScript">
<!--
function popWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}
//-->
</script>
ex: <a href="#" onclick="openWindow('someURL','','width=650,height=500')"></a>

i actually wanted to open a new window using the snippet above('a JS')... passing the value of PHP to JS upon clicking the selected article. i dont have problem doing it in PHP, i just wanted to use some JS to pop a Window cause i find it nice.

tanx for the reply... hope to received some input :)
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

You want to pass JS vars? to the JS in the new window or to the PHP you open in the pop-up?
victhour
Forum Commoner
Posts: 38
Joined: Fri Feb 17, 2006 12:59 am
Contact:

Post by victhour »

passing PHP values to JS (javascript) :)
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

victhour wrote:passing PHP values to JS (javascript) :)
So, if you want the simple way, take wyrmmage's.

However, if you want more sophisticated way, you can use AJAX to retrieve data from the server (PHP...).

http://developer.mozilla.org/en/docs/AJ ... ng_Started
wyrmmage
Forum Commoner
Posts: 56
Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID

Post by wyrmmage »

yep, either way works. I'd recommend going for Ajax if you're going to be doing a lot of this sort of thing, but if you're just going to do between ten to twenty vars, I'd go my way.
-wyrmmage
victhour
Forum Commoner
Posts: 38
Joined: Fri Feb 17, 2006 12:59 am
Contact:

Post by victhour »

:) ok, thanks.. il try to process wyrmmage way...
Post Reply