-2

i'm trying to learn some basic php, i've some difficult to get post value from a dropdown menu. I've that error :

Notice: Undefined index: mese in /opt/lampp/htdocs/ethermine/class.php on line 3

i don't know why, the index mese exist on my dropdown menu, it's name attribute of select tag like you can see on my code. So why i've that error? I see many many error on stackoverflow but it seems that the code is ok but of course i'm wrong.

test.php

<html>
<head>
<title>TEST</title>
</head>
<body>

<form action="class.php" method="POST">
    <select name="mese">
        <option value=""></option>
        <option value="01">Gennaio</option>
        <option value="02">Febbraio</option>
        <option value="03">Marzo</option>
        <option value="04">Aprile</option>
        <option value="05">Maggio</option>
        <option value="06">Giugno</option>
        <option value="07">Luglio</option>
        <option value="08">Agosto</option>
        <option value="09">Settembre</option>
        <option value="10">Ottobre</option>
        <option value="11">Novembre</option>
        <option value="12">Dicembre</option>
    </select>
<input type="submit" name="scelta" value="Scegli il mese" />
</form>

</body>
</html>

class.php

<?php
if (!isset($_POST["mese"])){
    $sceltaMese = $_POST["mese"];
    echo $sceltaMese;
}
?>

Thanks

tafazzi87
  • 439
  • 2
  • 5
  • 14

1 Answers1

1

In class.php

insert this:

if (isset($_POST["mese"])){
   $sceltaMese = $_POST["mese"];
   echo $sceltaMese;
 } else {
  //do something for error
 }

you made a mistake by using !. so if the post was not set it would do that. Now without ! if the post is set it will run those lines.

JamesBond
  • 312
  • 2
  • 17