Hello everyone,

I’m a complete beginner to php. I am trying to learn PHP through self-study and have reached an obstacle which I’ve not been able to overcome and need. The problem is that when I open my php form in a web browser the following error message appears.

Notice: Undefined index: firstname in C:\xampp\htdocs\form_1\CONTACT_DETAILS_dataentry.php on line 20

Notice: Undefined index: surname in C:\xampp\htdocs\form_1\CONTACT_DETAILS_dataentry.php on line 21

Notice: Undefined index: telephone_no in C:\xampp\htdocs\form_1\CONTACT_DETAILS_dataentry.php on line 22

Notice: Undefined index: emailaddress in C:\xampp\htdocs\form_1\CONTACT_DETAILS_dataentry.php on line 23

I'm using the following script:

<?php   
include "cdconnect.php"; ?> <body> <h1>Enter Design</h1> <form method="post"> Firstname<input type="text" name="firstname"><br> Surname:<input type="text" name="surname"><br> Telephone_no:<input type="text" name="telephone_no"><br> Emailaddress:<input type="text" name="emailaddress"><br> <input type="submit"> </form> <?php //variables get values from form $firstname = $_POST['firstname']; $surname = $_POST['surname']; $telephone_no = $_POST['telephone_no']; $emailaddress = $_POST['emailaddress']; $query = "INSERT INTO contact_details (firstname,surname,telephone_no,emailaddress) VALUES ( '$firstname', '$surname', '$telephone_no', '$emailaddress')"; mysql_query($query) OR die(mysql_error()); if(isset($_POST['firstname'])) { $firstname = $_POST['firstname']; } if(isset($_POST['surname'])) { $surname = $_POST['surname']; } if(isset($_POST['telephone_no'])) { $telephone_no = $_POST['telephone_no']; } if(isset($_POST['emailaddress'])) { $emailaddress = $_POST['emailaddress']; } mysql_CLOSE(); ?> </body> </html>

I'll be grateful for any assistance. Thanks

    You're not checking to see whether [font=monospace]$_POST['firstname'][/font] et al. exist before trying to use their values (they won't if, for example, you request the page without submitting a form). Use something like [man]isset[/man] to see if they do - and think about what you want to happen if they don't.

      Hello Weedpacket,

      Thanks for the advice.

      The problem has been resolved.

      Regards

        Jamdason, Do you mind posting the "fixed" code so that others, like myself, that are having the same issue can see the before and after?

        Thanks

          Hi Freshpeel,

          Placing the following code between the php opening tag and the variables resolved the problem:

          if(isset($_POST['submit']))//NAME OF THE SUBMIT BUTTON
          

          Actual example based on the initial script:

          <?php
          if(isset($_POST['submit']))//NAME OF THE SUBMIT BUTTON
          //variables get values from form
          $firstname = $_POST['firstname'];
          $surname = $_POST['surname'];
           

          Regards

            Write a Reply...