28

I'm new in PHP and I'm getting this error:

Notice: Undefined index: productid in /var/www/test/modifyform.php on line 32

Notice: Undefined index: name in /var/www/test/modifyform.php on line 33

Notice: Undefined index: price in /var/www/test/modifyform.php on line 34

Notice: Undefined index: description in /var/www/test/modifyform.php on line 35

I couldn't find any solution online, so maybe someone can help me.

Here is the code:

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
   <input type="hidden" name="rowID" value="<?php echo $rowID;?>">

   <p>
      Product ID:<br />
      <input type="text" name="productid" size="8" maxlength="8" value="<?php echo $productid;?>" />
   </p>

   <p>
      Name:<br />
      <input type="text" name="name" size="25" maxlength="25" value="<?php echo $name;?>" />
   </p>

   <p>
      Price:<br />
      <input type="text" name="price" size="6" maxlength="6" value="<?php echo $price;?>" />
   </p>

   <p>
      Description:<br />
      <textarea name="description" rows="5" cols="30">
      <?php echo $description;?></textarea>
   </p>

   <p>
      <input type="submit" name="submit" value="Submit!" />
   </p>
   </form>
   <?php
   if (isset($_POST['submit'])) {
      $rowID = $_POST['rowID'];
      $productid = $_POST['productid']; //this is line 32 and so on...
      $name = $_POST['name'];
      $price = $_POST['price'];
      $description = $_POST['description'];

}

What I do after that (or at least I'm trying) is to update a table in MySQL. I really can't understand why $rowID is defined while the other variables aren't.

Thank you for taking your time to answer me. Cheers!

Dyin
  • 5,815
  • 8
  • 44
  • 69
LPoblet
  • 465
  • 1
  • 4
  • 9
  • Do you have multiple `
    ` tags?
    – CodeCaster May 16 '12 at 07:07
  • do a print_r of whole $_POST array and see the index and values – Khurram Ijaz May 16 '12 at 07:13
  • Try to do this and see what you get `var_dump($_POST);` – guitarlass May 16 '12 at 08:12
  • @Mian_Khurram_Ijaz The Output shows only the rowID variable at the moment of submitting.Array ( [rowID] => Oranges [submit] => Submit! ) – LPoblet May 16 '12 at 09:00
  • @CodeCaster No, is the only form. – LPoblet May 16 '12 at 09:03
  • @guitarlass Same result than print_r($_POST), only $rowID is shown. – LPoblet May 16 '12 at 09:06
  • @LeandroPoblet Well there's really two separate questions here. As for why you're getting undefined index notices, those are easily googleable. Then the next question that that brings up (why aren't the form values submitting?) is likely not as simple. Anyway, the first possibility that comes to mind is that it may be invalid HTML. – Corbin May 16 '12 at 17:35
  • just to test why don't you remove `size="25" maxlength="25" value=""` codings and just type in a value for name and price and see what happens ? – guitarlass May 17 '12 at 04:57
  • Duplicate of http://stackoverflow.com/questions/4465728/php-error-notice-undefined-index and many others. – Cees Timmerman Dec 19 '13 at 11:32

9 Answers9

54

Try:

<?php

if (isset($_POST['name'])) {
    $name = $_POST['name'];
}

if (isset($_POST['price'])) {
    $price = $_POST['price'];
}

if (isset($_POST['description'])) {
    $description = $_POST['description'];
}

?>
Darren Shewry
  • 10,179
  • 4
  • 50
  • 46
Adam
  • 1,684
  • 14
  • 18
  • 7
    For those arriving here more recently, PHP 7 brings the null coalesce operator, so the example can be simplified: `$name = $_POST['name'] ?? 'any default value, but probably null';` – Michael Cordingley Nov 14 '17 at 17:41
6

Apparently the index 'productid' is missing from your html form. Inspect your html inputs first. eg <input type="text" name="productid" value=""> But this will handle the current error PHP is raising.

  $rowID = isset($_POST['rowID']) ? $_POST['rowID'] : '';
  $productid = isset($_POST['productid']) ? $_POST['productid'] : '';
  $name = isset($_POST['name']) ? $_POST['name'] : '';
  $price = isset($_POST['price']) ? $_POST['price'] : '';
  $description = isset($_POST['description']) ? $_POST['description'] : '';
Robert Wilson
  • 659
  • 1
  • 12
  • 28
3

This is happening because your PHP code is getting executed before the form gets posted.

To avoid this wrap your PHP code in following if statement and it will handle the rest no need to set if statements for each variables

       if(isset($_POST) && array_key_exists('name_of_your_submit_input',$_POST))
        {
             //process PHP Code
        }
        else
        {
             //do nothing
         }
  • Its a very logical answer given by Akshat Maltare. I had a similar issue on a totally different form and I used this post given and the form worked perfectly. Thank you Akshat. It sounded logical to me even before I tried it on my php form and yes, its good. I am referring to the PHP code executing before the form gets posted. – bytise Mar 19 '17 at 00:57
2

TRY

<?php

  $rowID=$productid=$name=$price=$description="";  

   if (isset($_POST['submit'])) {
      $rowID = $_POST['rowID'];
      $productid = $_POST['productid']; //this is line 32 and so on...
      $name = $_POST['name'];
      $price = $_POST['price'];
      $description = $_POST['description'];

}
sumish1985
  • 71
  • 6
0

There should be the problem, when you generate the <form>. I bet the variables $name, $price are NULL or empty string when you echo them into the value of the <input> field. Empty input fields are not sent by the browser, so $_POST will not have their keys.

Anyway, you can check that with isset().

Test variables with the following:

if(isset($_POST['key'])) ? $variable=$_POST['key'] : $variable=NULL

You better set it to NULL, because

NULL value represents a variable with no value.

Dyin
  • 5,815
  • 8
  • 44
  • 69
  • The variables are not empy. All input texts show the data retrieved form the db. What I try to do is to modify that data and update the db with the new values (of course after solving this problem, otherwise the db is updated erasing the row because there is no values in the variables after pressing "Submit!". – LPoblet May 16 '12 at 09:11
0

Hey this is happening because u r trying to display value before assignnig it U just fill in the values and submit form it will display correct output Or u can write ur php code below form tags It ll run without any errors

-1

If you are using wamp server , then i recommend you to use xampp server . you . i get this error in less than i minute but i resolved this by using (isset) function . and i get no error . and after that i remove (isset) function and i don,t see any error.

by the way i am using xampp server

Geroge
  • 1
  • 1
  • 2
-2

this error occurred sometime method attribute ( valid passing method ) Error option : method="get" but called by $Fname = $_POST["name"]; or

       method="post" but  called by  $Fname = $_GET["name"];

More info visit http://www.doordie.co.in/index.php

OpenWebWar
  • 580
  • 8
  • 16
-3

To remove this error, in your html form you should do the following in enctype:

<form  enctype="multipart/form-data">

The following down is the cause of that error i.e if you start with form-data in enctype, so you should start with multipart:

<form enctype="form-data/multipart">
xav
  • 5,452
  • 7
  • 48
  • 57
Omary
  • 1