1

This will be a very stupid question but I'm a newbie in programming and I just can't figure it out. What I want to do is make a color mixer in php. User can specify RGB values and then color should appear.

I have this:

<html>

  <head>
    <title>
      RGB
    </title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>

  <body>
    <form method="get">
      <center>
        <h3>
          RGB
        </h3>
      </center>
      <br>
      <h5>
        Red
      </h5>

      <input type="range" name="red" min="0" max="255">
      <h5>
        Green
      </h5>

      <input type="range" name="green" min="0" max="255">
      <h5>
        Blue
      </h5>

      <input type="range" name="blue" min="0" max="255">
      <br>
      <input type="submit" value="submit">
      <br>
      <br>
      <?php $red=$ _GET[ "red"]; $green=$ _GET[ "blue"]; $blue=$ _GET[ "green"]; function rgb2hex($red,$blue,$green) { return '#' . sprintf( '%02x', $red) . sprintf( '%02x', $blue) . sprintf( '%02x', $green); } rgb2hex($red,$blue,$green); ?>
    </form>
  </body>

</html>

But I keep getting info about undefined indexes and I don't know how to fix it.

George Netu
  • 2,758
  • 4
  • 28
  • 49
SuperM4n
  • 203
  • 4
  • 15
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – ʰᵈˑ May 11 '15 at 14:49

2 Answers2

0

The first run of this file won't have the $_GET['xxx'] values initialized as the form was not submitted. You can check if the form was submittled like this:

if (isset($_GET['myFormName'])){
    //your code here
}

You can set a name for your form and move your php code without the function inside of this if.

mmvsbg
  • 3,570
  • 17
  • 52
  • 73
0

You get the "Undefined Index.." because at the first open of the page $red, $blue and $green aren't setted but php code is executed even if that three variables doesn't exist.

You have to put all that php code into:

if(isset($_GET['submit_button_name']))
{
         //do your stuff here
}
Marco
  • 705
  • 8
  • 28