0


I encountered a very frustrating bug today (even though it's surely my fault) : I have an html form with several input elements which posts them to a php file. I keep on getting the undefined index error because one of the form element doesn't wanna get posted. Here's my html code (simplified) (/forms/form.php) :

<form action="/php/save.php" method="post">
    <table>
        <tr>
            <td><input type="text" value="" name="SpeechmanWeek1"/></td>
            <td><input type="text" value="" name="SpeechmanWeek2"/></td>
            <td><input type="text" value="" name="SpeechmanWeek3"/></td>
            <td><input type="text" value="" name="SpeechmanWeek4"/></td>
        </tr> 
        <tr>
            <td><input type="text" value="" name="ReaderWeek1"/></td>
            <td><input type="text" value="" name="ReaderWeek2"/></td>
            <td><input type="text" value="" name="ReaderWeek3"/></td>
            <td><input type="text" value="" name="ReaderWeek4"/></td>
        </tr> 
        <tr>
            <td><input type="submit" value="Submit" name="submit"/></td>
            <td></td>
            <td></td>
            <td></td>
        </tr> 
    </table>
</form>

Now, here's my php code (simplified too) (/php/save.php) :

<?php 
    if(isset($_POST['submit'])) {
        $SpeechmanWeek1 = $_POST['SpeechmanWeek1'];
        $SpeechmanWeek2 = $_POST['SpeechmanWeek2'];
        $SpeechmanWeek3 = $_POST['SpeechmanWeek3'];
        $SpeechmanWeek4 = $_POST['SpeechmanWeek4'];
        // the above code works fine (I get no undefined index error for it)
        // but the following code keeps on causing an undefined index notice and I don't get why 
        $ReaderWeek1 = $_POST['ReaderWeek1'];
        $ReaderWeek2 = $_POST['ReaderWeek2'];
        $ReaderWeek3 = $_POST['ReaderWeek3'];
        $ReaderWeek4 = $_POST['ReaderWeek4'];
    }
?>

I keep on getting this error :

Notice: Undefined Index: ReaderWeek1 in /var/www/html/php/save.php
Notice: Undefined Index: ReaderWeek2 in /var/www/html/php/save.php
Notice: Undefined Index: ReaderWeek3 in /var/www/html/php/save.php
Notice: Undefined Index: ReaderWeek4 in /var/www/html/php/save.php

I already had a look at other threads but none actually matched my situation. Do you have any idea why I get this error and why specific form elements don't get posted while all the others do ?
Thanks in advance for taking the time to answer me.

  • are you passing these fields through form submit? – Jigar Shah Jun 21 '17 at 06:40
  • Try to do `print_r($_POST);` and see what it displays – hungrykoala Jun 21 '17 at 06:40
  • Here's what it prints when I do `print_r($_POST);` :
    `Array ( [SpeechmanWeek1] => John [SpeechmanWeek2] => Vincent [SpeechmanWeek3] => Jackson [SpeechmanWeek4] => John [ReaderWeek1] => Rody [ReaderWeek2] => Jackson [ReaderWeek3] => John [ReaderWeek4] => Rody`
    – Rody Gosset Jun 21 '17 at 06:45
  • typo missing close parenthesis in this line if(isset($_POST['submit']) { – JYoThI Jun 21 '17 at 06:47
  • You didn't close the if statement if(isset($_POST['submit']). Either you posted a wrong code or by mistake you missed it. – Sehdev Jun 21 '17 at 06:48
  • Check your $_POST-Variables before you use them! – Bernhard Jun 21 '17 at 06:49
  • There is nothing wrong , i have tested locally. – Suraj Khanal Jun 21 '17 at 06:49
  • I mistyped, But this doesn't change the error I get – Rody Gosset Jun 21 '17 at 06:51
  • If that is what `print_r` displays then you don't have to worry about those notices. – hungrykoala Jun 21 '17 at 06:51
  • The thing is that I can't access the data the user entered in those input fields while I can access the data the user entered in the other input fields – Rody Gosset Jun 21 '17 at 06:53
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Epodax Jun 21 '17 at 07:12

1 Answers1

0

You have to check your $_POST-Variables before using them.

if($_POST['SpeechmanWeek1']){
 $SpeechmanWeek1 = $_POST['SpeechmanWeek1'];
}else{
 $SpeechmanWeek1 = 'whateveryouwant';
}
Bernhard
  • 1,852
  • 11
  • 19