Click to See Complete Forum and Search --> : PHP variables into javascript?


tomhilton
11-12-2003, 07:30 AM
Hi, I have a page that loads a small popup with images when you click on a link. I am using PHP/MySQL to pull info from a database, and javascript to pass an image variable to a popup page. In the header the following function is defined:

function meatloaf(page){
var openup
openup=window.open(page,'photopage','width=480,height=360,directories=yes,location=yes,menubar=yes,s crollbars=yes,status=yes,toolbar=yes,resizable=yes,screenX=25,screenY=100,top=25,left=100')
}

in the body I have a PHP loop that fetches a mysql array. within the loop is the following script:

<script language="Javascript">
<!-- Hide script from old browers
upload_id = <?php echo "$id" ?>;
destination = "single_photo.php?info="+upload_id;
// End hiding
</script>
<a href='#' onclick="meatloaf(destination)">

In this case, the loop scrolls through about 8 or so items, My problem is that no matter which anchor tag i click on, the last definition of "destination" is the one that pops up. Is there a better way to pass php variables into javascript so that each anchor tag will contain the correct javascript link?

Charles
11-12-2003, 07:38 AM
<a href="single_photo.php?info=<?php echo "$id" ?>" onclick="meatloaf(this.href); return false">

Scriptage
11-12-2003, 07:42 AM
function meatloaf(page){
page2 = "single_photo.php?info="+page
var openup
openup=window. open(page2,'photopage','width=480,height=360,direct
ories=yes,location=yes,menubar=yes,scrollbars=yes,
status=yes,toolbar=yes,resizable=yes,screenX=25,sc
reenY=100,top=25,left=100')
}

loop here:

<a href='#' onclick="meatloaf(<?php echo $id?>)">

end loop;

Scriptage
11-12-2003, 07:43 AM
go with charles' must've posted as I was writing the reply...Charles' looks alot nicer :D

Scriptage
11-12-2003, 07:45 AM
just a thought Charles...
<a href="single_photo.php?info=<?php echo "$id" ?>" onclick="meatloaf(this.href); return false">

wont php echo "$id" clash with the "" in the href?

Charles
11-12-2003, 08:02 AM
1) The relative neatness of the page is not important. The problem with your version is that it will give you a page that doesn't work for the 13% of users who do not use JavaScript.

2) The PHP pre-processor will only see the <?php echo "$id" ?> part and that part will have been replaced, quotes and all, by the time the page gets to the user agent.

Scriptage
11-12-2003, 08:05 AM
and i suppose onclick="meatloaf(this.href); return false will work without javascript ROFLMFAO

I know that the href still points to the image but the question was for a pop up not for a href.

tomhilton
11-12-2003, 07:44 PM
Thanks to both of you guys! I edited the anchor tag as Charles suggested, it works perfectly. Thanks again.

fredmv
11-13-2003, 01:24 AM
Originally posted by Scriptage
and i suppose onclick="meatloaf(this.href); return false will work without javascriptThe fact of the matter is that it's degradable. ;)