-2

i have an error with this code. I want to change the background with the radio buttons, some help pls

<?php 
    if (!empty($_GET)) {
        if ($_GET('kleur') == 'rood') {
            echo  '<body bgcolor="red">';
        }
        if ($_GET('kleur') == 'oranje') {
            echo  '<body bgcolor="orange">';
        }
        if ($_GET('kleur') == 'geel') {
            echo  '<body bgcolor="yellow">';
        }
        if ($_GET('kleur') == 'groen') {
            echo  '<body bgcolor="green">';
        }
        if ($_GET('kleur') == 'blauw') {
            echo  '<body bgcolor="blue">';
        }
    }
?>
DumbCoder
  • 5,696
  • 3
  • 29
  • 40
N kubo
  • 1
  • 1
  • 2

4 Answers4

1

Write a clean code.
Define array of possible colors:

$colorMap = [
    'rood' => 'red',
    'oranje' => 'orange',
    'geel' => 'yellow',
    'groen' => 'green',
    'blauw' => 'blue'
];

And get color which you need (instead of DEFAULT_COLOR set default color):

if (isset($_GET['kleur']) && isset($colorMap[$_GET['kleur']])) {
    $bgColor = $colorMap[$_GET['kleur']];
} else {
    $bgColor = DEFAULT_COLOR
}

Than output your html:

echo '<body style="background-color: '.$bgColor.'">';
Taron Saribekyan
  • 1,360
  • 7
  • 13
0

I think you have php syntax error

Here you are using if ($_GET('kleur') == 'rood') {

which is wrong it should be $_GET['kleur']

Make sure you do it in all your conditions. for more on $_GET function click here

Binit Ghetiya
  • 1,919
  • 2
  • 21
  • 31
0

replace this,

if ($_GET('kleur') == 'blauw')

to

if ($_GET['kleur'] == 'blauw')
Dhaval Naphade
  • 555
  • 2
  • 21
0

On your page body style you need to have a style attribute that will change based on clicked radio. and the easy way is to use switch.

    <body style="<?php echo $body_style;?>">


    </body>

<?php
$body_style=""; // so that we don't get undifined index on body style

    if(isset($_GET['kleur'])){

        switch ($_GET['kleur']) {
        case 'rood':

            $body_style = "background:red";

            break;

        case 'oranje':
            $body_style = "background:orange"
            break;

        case 'geel':
            $body_style ="background:yellow";
            break;

        case 'groen':
             $body_style="background:green"
            break;

        case 'blauw':
                $body_style="background:blue"
                break;
        default:
            $body_style="";
            break;
    }


    }


?>
Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34