Forum Moderators: open

Message Too Old, No Replies

Getting variable value from javascript to PHP

         

tintin99

9:27 am on Aug 2, 2008 (gmt 0)

10+ Year Member



Hi,

I'm using the Javascript Image Gallery from A List Apart which is working fine.

I need to get the filename for the currently selected main image from javascript into PHP and I can't work out how to do it. I'm OK with PHP but rather a beginner at Javascript so I suspect it's a simple job.

The script code is:

<script type="text/javascript" language="javascript">
function showPic (whichpic) {
if (document.getElementById) {
document.getElementById('placeholder')»
.src = whichpic.href;
if (whichpic.title) {
document.getElementById('desc')»
.childNodes[0].nodeValue = whichpic.title;
} else {
document.getElementById('desc')»
.childNodes[0].nodeValue = whichpic.childNodes[0].nodeValue;
}
return false;
} else {
return true;
}
}
</script>

and the link code for selecting an image is:

<ul>
<li><a onclick="return showPic(this)" href="images/bananas.jpg" title="A bunch of bananas on a table">some bananas</a></li>
<li><a onclick="return showPic(this)" href="images/condiments.jpg" title="Condiments in a Chinese restaurant">two bottles</a></li>
<li><a onclick="return showPic(this)" href="images/shells.jpg" title="Seashells on a table">some shells</a></li>
</ul>

As I see it I need to make the value of 'whichpic.href' available to my PHP code.

Can anyone help?

rocknbil

7:10 pm on Aug 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



document.getElementById('placeholder').src = 'yourscript.php?pic='+whichpic.href;

Not sure what you're trying to do, but you could do that and let yourscript.php supply the image. You would have the value of pic to do whatever else you wanted to do with PHP.

tintin99

9:18 pm on Aug 2, 2008 (gmt 0)

10+ Year Member



Thanks rocknbill - I should have clarified what I'm trying to do. I'm designing a client gallery for a photographer's site where the client will be able to order prints. I'm using PHP/MySQL for the web pages and shopping cart, but I like the fact that a javascript-based image gallery will change pics really fast (ie without reloading the page).

So the client clicks a thumbnail and gets a large version of the image with a link under it that says 'Order Print'. I need that link to pass the selected image number to the PHP cart script, but I can't work out how to get the variable 'whichpic.href' out of the javascript and into my PHP hyperlink.

Hope that makes sense!

londrum

9:26 pm on Aug 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you could get javascript to pass the name to a cookie, and get php to read the cookie.

(it doesn't matter which language wrote the cookie - it's just a text string. another language should still be able to read it.)

tintin99

9:45 pm on Aug 3, 2008 (gmt 0)

10+ Year Member



Brilliant! Thanks londrum - that worked!