SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot Matt_M's Avatar
    Join Date
    Sep 2004
    Location
    Oregon
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Change image when radio button is clicked?

    Hi,

    I would like to have an image change to a different one depending on which radio button is clicked. I want to use PHP instead of Javascript incase the user has Javascript disabled and this is an important part of the site. Can someone please help me out with the code for this?

    Thanks, Matt

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if ($_POST['radio_img'] == 'img1') {
      echo 
    "<img src='img1.jpg' />";
    } elseif (
    $_POST['radio_img'] == 'img2') {
      echo 
    "<img src='img2.jpg' />";
    } else {
      echo 
    "<img src='img3.jpg' />";


  3. #3
    SitePoint Evangelist
    Join Date
    Sep 2004
    Location
    Oregon
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You may want to note that the above code will only work if there is a form and the user hits submit.

    Remember, PHP is a server-side language. Thus, if you want to parse something on the client side you have to use a language like Javascript.

    If you want it so they click the radio button, and targets the image change then you will have to use Javascript. If you want it so they have to click the submit button (thus the page reload), then you can use a language like PHP.

  4. #4
    SitePoint Zealot Matt_M's Avatar
    Join Date
    Sep 2004
    Location
    Oregon
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhh, I see. I guess I'll have to use Javascript and come up with another solution for users that have it disabled. Thanks for the help!

  5. #5
    SitePoint Evangelist
    Join Date
    Sep 2004
    Location
    Oregon
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I honestly wouldn't worry too much if a user has javascript disabled. It does not happen too often and I would suspect a user would enable it to use your functions.

    I don't know Javascript however I've seen it where if you have javascript disabled it will display an image over where the javascript would be. In that case, you could possibly create a "go" button or something and then use the coding that the above user already explained.

    You obviously don't want them to HAVE to use a "go" button, however if they have JC disabled, it could be an alternative option.


Bookmarks

Posting Permissions

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