Firel
Firel

Reputation: 41

How do i change page elements with php?

I want to change an image using a GET variable and switch, but I do not know how to change the image. Here is my html/php.

<img name="foo" src="bar.gif" alt="foobar">
<?php
switch($_GET['ine']) {
case "foo"
> I dont know what to put here.
break;


case "bar"
> I dont know what to put here.
break;
}

?>

Upvotes: 0

Views: 136

Answers (2)

Twinfriends
Twinfriends

Reputation: 1987

You could do something like the this:

 <?php
    switch($_GET['ine']) {
    case "foo":
     echo'<img name="foo" src="bar.gif" alt="foobar">';
    break;


    case "bar":
    echo '<img name="foo" src="bar_2.gif" alt="foobar">'; // Another image
    break;

    default:
     echo "Something you want to do when no $_GET[] case matches;
    break;

    }

    ?>

Simply output the image in the case you want, so you can change the source of the image for each case. Also, add a default case. Really important, otherwise you'll get some errors.

Upvotes: 1

Marvin Fischer
Marvin Fischer

Reputation: 2572

You cannot dynamically change it with php after you have written it, but you can do it like this:

<?php
switch($_GET['ine']) {
case "foo"
echo '<img name="foo" src="bar1.gif" alt="foobar">';
break;


case "bar"
echo '<img name="foo" src="bar2.gif" alt="foobar">';
break;
}

?>

This way the $_GET['ine'] decides which one of both gets outputted

If you want it more dynamic you can actually go there and do this:

echo '<img name="foo" src="'.$_GET['ine'].'.gif" alt="foobar">';

But I advise you to read into input sanitizing first

Upvotes: 1

Related Questions