Jump to content
Jayfromsandiego

Form for Color Output not showing

Recommended Posts

I am working on a form that takes in 5 numbers as INPUT (the number range is from 1 to 10). Then, the OUTPUT will display the 5 numbers entered with their associated color. I am having problems getting the color value to show on the browser. Can someone please help as to what I am doing wrong?

Below is the code I have so far. 

<?php

error_reporting(0);
$position1 = $_POST['p1'];
$position2 = $_POST['p2'];
$position3 = $_POST['p3'];
$position4 = $_POST['p4'];
$position5 = $_POST['p5'];


 if ($_POST['clear'])
{
 $position1="";
 $position2="";
 $position3="";
 $position4="";
 $position5="";
 $message1="";
 $message2="";
 $message3="";
 $message4="";
 $message5="";
}
 if ($_POST['send'])
 

 {
$message1 = "P1:" ."\r\n" .$position1 ."\r\n" .$color1; // to create space we typed in ."\r\n"
$message2 = "P2:" ."\r\n" .$position2 ."\r\n" .$color2;
$message3 = "P3:" ."\r\n" .$position3 ."\r\n" .$color3;
$message4 = "P4:" ."\r\n" .$position4 ."\r\n" .$color4;
$message5 = "P5:" ."\r\n" .$position5 ."\r\n" .$color5;

$color1 = $position1;

  if ($position1 == "2,4,6,8,10")
    echo "Black";
     elseif ($position1 == "1,3,5,7,9")
      echo "Red";
       else
         echo "Number entered is not in game range.";

$color2 = $position2;

  if ($position2 == "2,4,6,8,10")
    echo "Black";
      elseif ($position2 == "1,3,5,7,9")
        echo "Red";
          else
            echo "Number entered is not in game range.";

$color3 = $position3;

   if ($position3 == "2,4,6,8,10")
     echo "Black";
       elseif ($position3 == "1,3,5,7,9")
         echo "Red";
           else
             echo "Number entered is not in game range.";

$color4 = $position4;

    if ($position4 == "2,4,6,8,10")
      echo "Black";
        elseif ($position4 == "1,3,5,7,9")
          echo "Red";
            else
              echo "Number entered is not in game range.";

$color5 = $position5;

    if ($position5 == "2,4,6,8,10")
      echo "Black";
        elseif ($position5 == "1,3,5,7,9")
          echo "Red";
            else
              echo "Number entered is not in game range.";

 }

?>
</div>

   <!-- START :: Input Form -->
<div align="center">
<form action = "" method = "POST">
<body>
<table border="0">
<tr>
 <td> P1: </td>
 <td> <input type = text name = p1 value = "<?php echo $position1;?>" size="2" autofocus></td>
 <td> P2: </td>
 <td> <input type = text name = p2 value = "<?php echo $position2;?>" size="2"></td>
 <td> P3: </td>
 <td> <input type = text name = p3 value = "<?php echo $position3;?>" size="2"></td>
 <td> P4: </td>
 <td> <input type = text name = p4 value = "<?php echo $position4;?>" size="2"></td>
 <td> P5: </td>
 <td> <input type = text name = p5 value = "<?php echo $position5;?>" size="2"></td>
<tr>
  <td colspan="10">&nbsp;</td>
</tr>
<tr>
 <td colspan="10" align="center">
  <input type = "submit" name="send" value = "GET POSITION COLORS" title="Click here to display color values.">
  <input type = "submit" name="clear" value = "RESET" title="Click here to clear text boxes.">
<?php
echo "<br><br>";
echo "<font face='arial' size='4'>";
echo $message1;
echo "<br>";
echo $message2;
echo "<br>";
echo $message3;
echo "<br>";
echo $message4;
echo "<br>";
echo $message5;
echo "</font>";
?>
 </td>
</tr>
</table>
</form>
 </div>
   <!-- END :: Input Form -->

</body>
</html>

----- EOF ----

examples.jpg

Share this post


Link to post
Share on other sites

It would be easier to read if you used the code icon (<>) and specified PHP. However, as best I can tell there is no place where you set the color in the HTML. I would expect to see "style" tags with the color to be output.

Share this post


Link to post
Share on other sites
<?php
error_reporting(0);
$position1 = $_POST['p1'];
$position2 = $_POST['p2'];
$position3 = $_POST['p3'];
$position4 = $_POST['p4'];
$position5 = $_POST['p5'];


 if ($_POST['clear'])
{
 $position1="";
 $position2="";
 $position3="";
 $position4="";
 $position5="";
 $message1="";
 $message2="";
 $message3="";
 $message4="";
 $message5="";
}
 if ($_POST['send'])
 
 {

$message1 = "P1:" ."\r\n" .$position1; // to create space we typed in ."\r\n"
$message2 = "P2:" ."\r\n" .$position2;
$message3 = "P3:" ."\r\n" .$position3;
$message4 = "P4:" ."\r\n" .$position4;
$message5 = "P5:" ."\r\n" .$position5;

 }

?>
</div>

   <!-- START :: Input Form -->
<div align="center">
<form action = "" method = "POST">
<body>
<table border="0">
<tr>
 <td> P1: </td>
 <td> <input type = text name = p1 value = "<?php echo $position1;?>" size="2" autofocus></td>
 <td> P2: </td>
 <td> <input type = text name = p2 value = "<?php echo $position2;?>" size="2"></td>
 <td> P3: </td>
 <td> <input type = text name = p3 value = "<?php echo $position3;?>" size="2"></td>
 <td> P4: </td>
 <td> <input type = text name = p4 value = "<?php echo $position4;?>" size="2"></td>
 <td> P5: </td>
 <td> <input type = text name = p5 value = "<?php echo $position5;?>" size="2"></td>
<tr>
  <td colspan="10">&nbsp;</td>
</tr>
<tr>
 <td colspan="10" align="center">
<p>
  <input type = "submit" name="send" value = "GET POSITION COLORS" title="Click here to display color values.">
  <input type = "submit" name="clear" value = "RESET" title="Click here to clear text boxes.">
</p>
<?php
echo "<br><br>";
echo "<font face='arial' size='4'>";
echo $message1 ."\r\n" .$color1;
$color1 = $_REQUEST['$position1'];

  if ($position1 == "2,4,6,8,10")
    echo "Black";
     elseif ($position1 == "1,3,5,7,9")
      echo "Red";
       else
         echo "This number is not in game range.";
echo "<br>";

echo $message2 ."\r\n" .$color2;
$color2 = $_REQUEST['$position2'];

  if ($position2 == "2,4,6,8,10")
    echo "Black";
      elseif ($position2 == "1,3,5,7,9")
        echo "Red";
          else
            echo "This number is not in game range.";
echo "<br>";

echo $message3 ."\r\n" .$color3;
$color3 = $_REQUEST['$position3'];

   if ($position3 == "2,4,6,8,10")
     echo "Black";
       elseif ($position3 == "1,3,5,7,9")
         echo "Red";
           else
             echo "This number is not in game range.";
echo "<br>";

echo $message4 ."\r\n" .$color4;
$color4 = $_REQUEST['$position4'];

    if ($position4 == "2,4,6,8,10")
      echo "Black";
        elseif ($position4 == "1,3,5,7,9")
          echo "Red";
            else
              echo "This number is not in game range.";
echo "<br>";

echo $message5 ."\r\n" .$color5;
$color5 = $_REQUEST['$position5'];

    if ($position5 == "2,4,6,8,10")
      echo "Black";
        elseif ($position5 == "1,3,5,7,9")
          echo "Red";
            else
              echo "This number is not in game range.";
echo "</font>";
?>
 </td>
</tr>
</table>
</form>
 </div>
   <!-- END :: Input Form -->

</body>
</html>

 

Edited by Jayfromsandiego
Inserting in PHP Code View

Share this post


Link to post
Share on other sites

It appears to me that you need to learn more about HTML. Using "\r\n" does not reliably create a new line, you use "<br />" instead. If you want to set a color you need something like <font style="color:red">.

Edited by gw1500se

Share this post


Link to post
Share on other sites

I don't need a break tag, I just need to give the output some spacing.

Also, lets forget about the actual color appearing on the browser. I'm not concerned with that part. I only need help with the PHP code that provides the actual words either "Black" or "Red" to appear beside each number. (Even numbers are BLACK and odd numbers are RED) I can't seem to figure out why it will not work. 🙁

 

Share this post


Link to post
Share on other sites

 

24 minutes ago, Jayfromsandiego said:

$message1 = "P1:" ."\r\n" .$position1; // to create space we typed in ."\r\n"

Apparently a space character, or one of the HTML spacing options (&nbsp; &ensp; &emsp;) isn't good enough.

Share this post


Link to post
Share on other sites

One way

<?php
$fields = ['p1', 'p2', 'p3', 'p4', 'p5'];
$valid_nums = range(1,10);
$output = '';

if (isset($_GET['p1'])) {
    foreach ($fields as $f) {
        if (!in_array($_GET[$f], $valid_nums)) {
            $clr = 'Not in range';
            $cls = '';
        }
        else {
            if ($_GET[$f]%2 ==0) {
                $clr = 'Black';
                $cls = 'class="even"';
            }
            else {
                $clr = 'Red';
                $cls = 'class="odd"';
            }
        }
        $output .= sprintf("%s <span %s>%2d %s</span><br>", strtoupper($f), $cls, $_GET[$f], $clr);
    }
}
                                                       
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Sample</title>
<style type="text/css">
    body  { font-family: verdana,sans-serif; font-size: 12pt; padding: 20px 50px; }
    .even { color: black; font-weight: 600;}
    .odd  { color: red; font-weight: 600;  }
</style>
</head>
<body>
    <?=$output?>
    <hr>
    <form>
        P1 <input type="text" name="p1" size="5"><br>
        P2 <input type="text" name="p2" size="5"><br>
        P3 <input type="text" name="p3" size="5"><br>
        P4 <input type="text" name="p4" size="5"><br>
        P5 <input type="text" name="p5" size="5"><br><br>
        <input type="submit" name="btnSub" value="Submit">
    </form>
</body>
</html>

Sample output:

image.png.92c2cb89fa4f2910e685a9e8db36fb0d.png

Share this post


Link to post
Share on other sites

Sorry, I misunderstood what you are asking. I think your problem is here:

if ($position1 == "2,4,6,8,10")

Are you expect this to return true/false if $postion1 matches any one of those? That will only return true if $position1 matches that entire string. You need to use in_arry() with one for the even numbers and one for the odd.

Edited by gw1500se

Share this post


Link to post
Share on other sites

Barand.

You are the man. You have outdone yourself again. That's exactly what I was trying to code. Thank you so much. 😁 

Now I need to dive in this code and learn exactly where everything goes and why it works. 

gw1500se your advice is appreciated as well. My head is upside down right now I think I need to take a break and step away from the code for a while before I return to it.

Edited by Jayfromsandiego

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.