Welcome to WebmasterWorld Guest from 54.160.254.203

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP and Javascript help

Passing a PHP variable to a Javascript popup. How?

     
12:33 am on May 4, 2006 (gmt 0)

New User

5+ Year Member

joined:May 3, 2006
posts:3
votes: 0


Here's an easy one, or at least I think it should be.

I need to pass a php variable to a pop-up opened using javascript.

$picone1 is a pic name ramdomly selected from a mysql database, I then use $picone1 to display a thumbnail pic which is a link to a pop up page which shows a larger version of that pic.

Here's what I have on the main page;
<a href=# onclick=window.open('viewpic.php?piconel','newpage',width=350,height=350'); return false> </a>

On viewpic.php, I simply have this;
<img src=$piconel>

I'm sure it's probably a tad more complicated then that. It's not working, and any help would be much appreciated

12:42 am on May 4, 2006 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


Welcome to WebmasterWorld RichtorScript,

if I understand correctly you could use php to write the link

<a href=# onclick=window.open('viewpic.php?<?php echo $picone1;?>','newpage',width=350,height=350'); return false> </a>

I think that is what you mean

<added>I notice you use both $piconel and $picone1 are they different vars?

5:12 am on May 4, 2006 (gmt 0)

New User

5+ Year Member

joined:May 3, 2006
posts:3
votes: 0


Hey, thanks for the welcome!
And of course thanks for your help.

So I got it to work on a test page, but when I try to implement it on the actual page, which randomly selects 4 pics and displays them, it doesn't seem to work. I think this is because I'm using it in between an echo statment. Here's what I'm looking at.

$random = mt_rand(0,$row-1)+1;
while(in_array($random,$randomNumbers)) {
$random = mt_rand(0,$row-1)+1;
}
array_push($randomNumbers,$random);
if ($result){
while ($r = mysql_fetch_array($result)) {
if ($i == 0 AND $r[index] == $random){
$picone = $r["spic"];
$piconel = $r["lpic"];
echo "
<div id=pic1> <a href=# onclick=window.open('viewpic.php?piconel=<?php echo $piconel;?>','CSPL','width=350,height=350'); return false>
<img src= $picone width=86 height=71></a>
</div>";
}else if ($i == 1 AND $r[index] == $random){
$pictwo = $r["spic"];
echo "
<div id=pic2>
<img src= $pictwo width=86 height=71>
</div>";
}else if ($i == 2 AND $r[index] == $random){
$picthree = $r["spic"];
echo "
<div id=pic3>
<img src= $pic3 width=86 height=71>
</div>";
}else if ($i == 3 AND $r[index] == $random){
$picfour = $r["spic"];
echo "
<div id=pic4>
<img src= $picfour width=86 height=71>
</div>
";
}}
mysql_free_result($result);
}else{
echo "No data";
}
Is there a work around or a better way to set it up?

7:09 am on May 4, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 7, 2003
posts:1230
votes: 0


hi RichtorScript,

does your script on the other-then-test system returns your "no data" message?

if so, your database request is not working.

if not, please tell with more detail, what does your script output and where the problem is. it might be within the viewpic.php for example. it might be that the php configuration between your test and life system differ (register globals for example) and that's why it is not working etc. so please some more info to track down the problem.

--hakre

4:35 pm on May 4, 2006 (gmt 0)

New User

5+ Year Member

joined:May 3, 2006
posts:3
votes: 0


Thanks for the help, but I got that part figured out. As far as I can tell it was becuase I using a <?PHP?> call in the middle of a an echo statement. As soon as I took it out and worked.

Now another question, can I send two variables this is what I was trying;

<a href="#" onclick="window.open('viewpic.php?piconel=<?php echo $piconel;?>?pic1text=<?php echo $pic1text;?>','newpage','width=350,height=350'); return false">
<img src= "pic.jpg" width=86 height=71>
</a>

5:06 pm on May 4, 2006 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


>> can I send two variables

yes, what you have there should work

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members