Skip to content
Advertisement

Display a php variable’s color inside , depending on the value

I want to scan the variable value $answer, and depending if it’s bigger than 6, to be displayed as green, otherwise, as red.

<?php
$answer = '';
if (!empty ($_POST)) {
  numbers.$answer = ($_POST['text1'] + $_POST['text2'] + _POST['text3'])/3;
}
?>
<p class='field'>
  <label for='pass'>Media Bacalaureat</label>
  <input type="number" name="text4" value="<?php echo $answer; ?>" 
  placeholder="Rezultat" disabled="disabled">
  <span id='valida' class='i i-close'></span>
</p>

thank you

Advertisement

Answer

If you mean color green or red, here you go…

<?php
$answer = ""; 
if(isset($_POST)){
  $answer = ($_POST['text1'] + $_POST['text2'] + $_POST['text3'])/3;
} else {
  // error code here
  // exit("no answer was provided");
}
if(isset($answer)){
  if($answer > '6'){
    $color = 'green';
  } else {
    $color = 'red';
  }
}
?>
<html>
  <head>
  </head>
  <body>
    <p class='field'>
      <label for='pass'>Media Bacalaureat</label>
      <input style="color:<?php echo $color;?>;" type="number" name="text4" value="<?php echo $answer;?>" placeholder="Rezultat" disabled="disabled">
      <span id='valida' class='i i-close'></span>
    </p>
  </body>
</html>

Preview: https://3v4l.org/0ejhC

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement