images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ben_albrechts
Forum Commoner
Posts: 33
Joined: Wed Oct 26, 2005 3:33 am

images

Post by ben_albrechts »

Can somebody help me please , i'm new to the whol php-programming thing.
I have a form on my website for my visitors , and depending on what they submit through the form I want a different picture/image to be shown.

so it shud be an if construction that basically does :
if blue then show the blue image , but how do i do that?

Thank you
mickd
Forum Contributor
Posts: 397
Joined: Tue Jun 21, 2005 9:05 am
Location: Australia

Post by mickd »

Code: Select all

if($_POST['image'] == 'blue') {
echo '<img src="blue.jpg" alt="Blue" />';
} elseif($_POST['image'] == 'red') {
echo '<img src="red.jpg" alt="Red" />';
}

// etc
or you could use the switch command instead

PHP Manual - Switch
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Or something like:

Code: Select all

<?php

$images = array('blue', 'red', 'green', 'yellow', 'orange');

if (in_array($_POST['image'], $images)) {
    echo "<img src=\"{$_POST['image']}.jpg\" />";
} else {
    echo "<img src=\"default.jpg\" />";
}

?>
But do be careful when using user input, assume it is malicious in all circumstances. :)
ben_albrechts
Forum Commoner
Posts: 33
Joined: Wed Oct 26, 2005 3:33 am

Post by ben_albrechts »

thank you both. I'll try your ideas later n tonight , and if i have any more problems i'll be back with more questions :D
Post Reply