Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Combining Javascript/PHP with onClick

    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.

    Genia

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,449
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

    <?php
    if ($index_value == "gallery1-1")
    {
    }
    elseif ($index_value == "gallery1-2")
    {
    }
    else...
    ?>


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

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    Quote Originally Posted by gjanssen View Post
    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.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,449
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    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)???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,449
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •