-1

this might be a stupid question, but I'm doing simple form for editing data in database in PHP, but I can't figure out, where is mistake, it only keeps telling me, that I have undefined index

<table>
     <tr>

                 <td><input type="text" name="first"></td>
                 <td><input type="text" name="sur"></td>
     </tr>
     <tr>       
                 <td><input type="submit" name="edit" value="Firstname":</td>
                 <td><input type="submit" name="edit" value="Surname"></td>
     </tr>

<?php


                  if($_POST['edit']=='Firstname')
                  {
                      $firstchange=$_POST['first'];

                  }
                  elseif($_POST['edit']=='Surname') 
                  {
                      $surchange=$_POST['sur'];
                  }

?>


Notice: Undefined index: edit in edit.php on line 177

Notice: Undefined index: edit in edit.php on line 183


This happens when I open the page. And I'm dissapointed I couldn't find what's wrong.

kollissx
  • 1
  • 2
  • Remember when you first open the page..... the form has not been submitted..... and therefore NON of the `$_POST` array exists. That only happens when the user presses one of your buttons – RiggsFolly Jun 04 '17 at 16:29
  • Which is why you will see most PHP code that processes form input checking to see if the form has been submitted before attempting to do anything with the submitted data. See `isset()` in the manual – RiggsFolly Jun 04 '17 at 16:30

1 Answers1

0

You will get this error every time the page is visited unless it is via a <form method="post"> with an input of ?edit=x&first=x&sur=x.

$_POST is an array of all the inputs in a form submitted to that url via a post. It is empty if you visit the page normally (with a GET).

Luke Mlsna
  • 468
  • 4
  • 16