View Full Version : Combining Javascript/PHP with onClick

11-15-2010, 10:45 PM
I am trying to set a PHP variable but am using the onClick() event handler, which requires JavaScript. See below:

[ICODE]<a href="javascript:update('gallery/Fgallery1-1.jpg', 0);" onClick="index_value = 'gallery1-1';">
<img src="gallery/gallery1-1.jpg" alt="Antique Impressions Hardwood Flooring - White Oak" border="0" class="galborders" onMouseOver="alert_msg('Click to view image close up')" onMouseOut="away()"></a><br /></ICODE]

The webpage is http://www.hardwood-floor-products.com/gallery1.php.

What I am trying to do is to set up a PHP variable, $index, that is equal to "gallery1-1', 'gallery1-2', etc. so that I can use that varialble throughout this module. I also realize that I need to use JavaScript with onClick() but I have had no success. I realize that PHP is servier-side and JavaScript is client-side, but there must be a way to do that.


Old Pedant
11-15-2010, 10:56 PM
What page(s) is that value suppose to carry over to? How does the customer *get* to one of those page(s)?

Generally, it would be simplest to avoid sending the index_value to the PHP code until a customer changes to another PHP page. And then you'd simply include that value along with whatever other info you send in the process of moving to the other PHP page.

11-16-2010, 02:52 PM
That is not what I am trying to do. There are 3 thumbnails on the left of the page. Clicking on one shows a corresponding full-size image on the right column. What I am trying to do is to make the paragraphs and the color chart change, depending on which thumbnail is clicked. So, I was using the onClick() function to set the index_value variable. However, then I need to configure a PHP variable that is equal to the Javascript index_value.

Then I need to write some PHP code below the image to the right that would open a text file with the appropriate paragrach content as well as put an image with the solors and sizes right below the paragraphs. In other words...

if ($index_value == "gallery1-1")
elseif ($index_value == "gallery1-2")

How can I set $index_value using a combination of Javascript and PHP?

11-16-2010, 06:50 PM
How can I set $index_value using a combination of Javascript and PHP?

in your case you donít. what you describe can (and should) be done with JavaScript alone.

Old Pedant
11-16-2010, 07:10 PM
Then I need to write some PHP code below the image to the right that would open a text file...

HOW???? You can't just have PHP suddenly re-appear in the middle of an ALREADY-OPEN-IN-THE-BROWSER page!

Well, you can, but not by simply invoking another PHP page.

You would need to
(a) use an <iframe>, and then use PHP to pull content into that <iframe>.
(b) use AJAX, so that your JavaScript code hits your PHP server and asks for content to put there.

The alternative is to simply bring in a brand new COMPLETE page from PHP, but I'd assume you don't want to do that.

So which will you do, (a) or (b)???

Old Pedant
11-16-2010, 07:11 PM
I should have refreshed the page sooner. Yes, I agree with Dormilich. There's really no reason to involve PHP for something this simple. But if you must do so, then *probably* you want an AJAX answer.