Page 1 of 1

Passing values('sessions') from PHP to javascript

Posted: Sun Dec 03, 2006 3:49 am
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]

Posted: Sun Dec 03, 2006 8:29 am
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]

Posted: Sun Dec 03, 2006 10:03 pm
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]

Posted: Sun Dec 03, 2006 11:08 pm
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 :)

Posted: Mon Dec 04, 2006 4:13 am
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?

Posted: Mon Dec 04, 2006 4:49 am
by victhour
passing PHP values to JS (javascript) :)

Posted: Mon Dec 04, 2006 9:07 am
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

Posted: Mon Dec 04, 2006 12:22 pm
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

Posted: Tue Dec 05, 2006 4:59 am
by victhour
:) ok, thanks.. il try to process wyrmmage way...