mardi 20 juillet 2021

PHP combining if/elseif statement with html shows wrong line [duplicate]

I am trying to make a page that shows different things depending on a html form submission. Problem is that I included an if/elseif/elseif statement and it shows only the last elseif part everytime after submitting the form. The things it needs to do:

  1. Start by showing the form to fill in (in code the first if())
  2. Show line when the form is submitted AND all form fields are filled (after first elseif())
  3. Show warning after form is submitted BUT NOT ALL form fields are filled (third elseif())

What currently happens is that after submitting and filling in all form field, it shows the warning that not all form fields are filled in. Here is a mock-up of the structure in my code:

<!DOCTYPE html>
<html>
    <head>
        <title>random title</title>
    </head>
    <body>
<?php 
    if(!isset($_POST['submit'])){ 
?>
        <h2>Fill in form</h2>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <!-- Form field here with fields for $data1 $data2 and $data3-->
        </form>
<?php 
        // Variables to hold form data after submit
        $form_filled = ($data1 && $data2 && $data3);
    } elseif(isset($_POST['submit']) && $form_filled) {
        // Code to run after form is submitted && all fields are filled
?>        
        <h3><!-- Shows a line only after form is submitted && all fields are filled--></h3>
<?php 
    } elseif(!$form_filled) { 
?>
        <h1><!-- Show line ONLY if form is submitted but NOT all fields are filled --></h1>
<?php 
} 
?>
    </body>
</html>

Aucun commentaire:

Enregistrer un commentaire