Begginer help form php

Hello, I am a begginer and I want to use a form in my website, I want the form and answer (php) to appear on the same page, but there is some error :
Notice: Undefined index: nom in C:\Users\user\Desktop\PROJET\la ragon\www\inscription.php on line 60

Notice: Undefined index: prenom in C:\Users\user\Desktop\PROJET\la ragon\www\inscription.php on line 61

Notice: Undefined index: age in C:\Users\user\Desktop\PROJET\la ragon\www\inscription.php on line 62

Notice: Undefined index: date in C:\Users\user\Desktop\PROJET\la ragon\www\inscription.php on line 63

I don’t know what I have to change, here is a part of the php file (with the form and php code), some text are in french…

How to add php tag ? the code is not displayed correctly…

<php>
<form action="" method="post">
               <label>Nom</label> :
               <input type="text" required="required" pattern="[a-zA-Z]{1,}" name="nom" />

               <label>Prénom</label> :
               <input type="text" required="required" pattern="[a-zA-Z]{1,}" name="prenom" />
               <input type="date" required="required" name="date" />
               <label>Age</label> :
               <input type="number" required="required" name="age" />
               <input type="submit" value="Envoyer" />
            
<?php
date_default_timezone_set('Europe/Madrid');
    $n=$_POST['nom'];
    $p=$_POST['prenom'];
    $q=$_POST['age'];
    $r=$_POST['date'];
$sup=40;
setlocale(LC_TIME, 'french');


if ($q > $sup) {
  echo "Bonjour ".$p." ".$n.",vous etes inscrit pour le ".strftime('%A %d %B %Y', strtotime($r))." et vous avez ".$q." ans donc vous ne payez rien .";
} elseif ($q == $sup){
    echo "Bonjour ".$p." ".$n.",vous etes inscrit pour le ".strftime('%A %d %B %Y', strtotime($r))." et vous avez ".$q." ans donc vous ne payer rien.";
}
else{
echo "Bonjour ".$p." ".$n.",vous etes inscrit pour le ".strftime('%A %d %B %Y', strtotime($r))." et vous avez ".$q." ans donc vous payez 10 euros.";
}
?>
</form>

</php>

What a mess… i just cleaned a bit and added some recommendations. …

<?php
// ALWAYS START WITH PHP AND END WITH THE VIEW (where the HTML appears)

date_default_timezone_set('Europe/Madrid');

// NOW give all the variables under in the IF statement a default value to prevent warnings

$n = '';
$p = '';
$q = '';
$r = '';
$msg = '';

// check if the form is sumbitted by chekking if the request method used is POST instead of GET
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    // overwrite values with post vars
    $n = $_POST['nom'];
    $p = $_POST['prenom'];
    $q = $_POST['age'];
    $r = $_POST['date'];

    $sup = 40;
    setlocale(LC_TIME, 'french');


    if ($q > $sup) {
        $msg = "Bonjour " . $p . " " . $n . ",vous etes inscrit pour le " . strftime('%A %d %B %Y', strtotime($r)) . " et vous avez " . $q . " ans donc vous ne payez rien .";
    } elseif ($q == $sup) {
        $msg = "Bonjour " . $p . " " . $n . ",vous etes inscrit pour le " . strftime('%A %d %B %Y', strtotime($r)) . " et vous avez " . $q . " ans donc vous ne payer rien.";
    } else {
        $msg = "Bonjour " . $p . " " . $n . ",vous etes inscrit pour le " . strftime('%A %d %B %Y', strtotime($r)) . " et vous avez " . $q . " ans donc vous payez 10 euros.";
    }
}


// Under the php closing tag start your output and dont do any PHP logic anymore. Only use PHP to echo your variables
?>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
    </head>
    <body>
        <div><?php echo $msg; ?></div>
        <form action="" method="post">
            <label>Nom</label> :
            <input type="text" required="required" pattern="[a-zA-Z]{1,}" name="nom" value="<?php echo $n; ?>" />

            <label>Prénom</label> :
            <input type="text" required="required" pattern="[a-zA-Z]{1,}" name="prenom" value="<?php echo $p; ?>" />
            <input type="date" required="required" name="date" value="<?php echo $r; ?>" />
            <label>Age</label> :
            <input type="number" required="required" name="age" value="<?php echo $q; ?>" />
            <input type="submit" value="Envoyer" />
        </form>
    </body>
</html>
2 Likes

Thanks for your help ! I have an another question : I want the message to appear in a little frame/window in the the same page with a cross to close the window, how to do that please ?

Sponsor our Newsletter | Privacy Policy | Terms of Service