html – If statement in a form PHP-ThrowExceptions

Exception or error:

I’m working on a code which has a form. The answer on the form needs to decide to which page it should go, but it doesn’t work.

<html>  
<body>



<form action= "<?php echo $action; ?>" method="post">
Temperatuur: <input type="text" name="graden"><br>
<input type="Submit">

           <?php
    if($_GET["graden"]>=28){
        $action = "Koelbox.php";
    }
    else{
        $action = "scrabble.php";
    };
    ?> 
</form>



</body>
</html> ```

How to solve:

You have to declare and initialize the variable before using it.

<?php
  if($_GET["graden"]>=28)
  {
    $action = "Koelbox.php";
  }
  else
  {
    $action = "scrabble.php";
  }
?> 

<form action= "<?php echo $action; ?>" method="post">
  Temperatuur: <input type="text" name="graden"><br>
  <input type="Submit">   
</form>

First check the GET parameter, then set your $action variable accordingly and then use it in your echo.

###

I just initialized the variable before the form, but as far as I see the IF statement doesn’t see the input in the form.

<?php
  if($_GET["graden"]>=28)
  {
    $action = "Koelbox.php";
  }
  else
  {
    $action = "scrabble.php";
  }
?> 

<form action= "<?php echo $action; ?>" method="post">
  Temperatuur: <input type="text" name="graden"><br>
  <input type="Submit">   
</form>

Leave a Reply

Your email address will not be published. Required fields are marked *