0

I get Undefined index on the 6th line (if condition). What is the reason?

<select name="day" id="day">
<?php
for($i=01; $i<=31; $i++)
{
    echo "<option value='$i'";
    if ($_POST['day'] == $i){ echo "selected";}
    echo">$i</option>";
}
 ?>
</select>
Elon Than
  • 9,603
  • 4
  • 27
  • 37

2 Answers2

0

That's beacuse $_POST['day'] is not set.

aperpen
  • 718
  • 7
  • 10
0

The message is crystal clear: the key day does not (always) exist in $_POST.

So you have to check if it does first:

if (isset($_POST['day']) && ($_POST['day']==$i)) { 
    echo "selected";
}
arkascha
  • 41,620
  • 7
  • 58
  • 90
  • this line ImageFile give me the same error what is the correct syntax to write? if($_FILES['ImageFile']['tmp_name']!="" && isset($_POST['L1'])) – faed mohammed Jan 28 '17 at 11:51
  • @faedmohammed Well, what about `if(isset($_FILES['ImageFile']) && ($_FILES['ImageFile']['tmp_name']!="") && isset($_POST['L1']))`? – arkascha Jan 28 '17 at 12:00
  • ok works fine solved! thk. – faed mohammed Jan 28 '17 at 12:02
  • @faedmohammed It is important that you understand _why_ this works and what the actual cause of those warnings is... Just copying some suggestion does not really help you to learn... – arkascha Jan 28 '17 at 12:04
  • same error on username and after using your method result Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) on code: if($_POST['UserName']!="" && $_POST['Password1']!="" && $_POST['Password2']!="" && $_POST['Email']!="" && $_POST['Name'] !="" && $_POST['Country'] !="" && $_POST['State'] !="" && $_POST['City'] !=""&& $_POST['ZipCode'] !="" && $_POST['Adress'] !="" && $_POST['idnumber']!=""&& $_POST['phone']!="" && $_FILES['ActImage']['tmp_name']!="" && $_FILES['RImage']['tmp_name']!="" && $_POST['checkbox']!="") – faed mohammed Jan 28 '17 at 12:30
  • This is what I feared: you did _not_ bother to understand what has been shown to you, you just try to copy it blindly. I suggest you start learning and understanding. Start by reading the documentation: http://php.net/manual/en/function.isset.php – arkascha Jan 28 '17 at 12:39
  • solved: if(isset($_POST['UserName']) && ($_FILES['UserName']!="") && $_POST['Password1']!="" && $_ – faed mohammed Jan 28 '17 at 12:43