-2

I am trying to learn PHP (just a tiny bit for a tiny project). I am trying to follow this tutorial, but when I do, it does not work. First of all I coppied the exact code there and it still did not work, but here is some of my code:

    <?php
         if($_POST['projectSubmit'] == "Submit") 
        {
            $toApprove= $_POST['projectName'];
        }
    ?>


    <form action ="getData" method="post" >
        <input type="text" name="projectName">
        <input type="submit" name="projectSubmit" value="Submit">
    </form>

Yet I get an error:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: projectSubmit

Filename: views/ViewProjectApproval.php

Line Number: 13

What am I doing wrong ?

BTW: Is this a correct way of transmitting data back to my controller ? (model view controller with code igniter)

Kalec
  • 2,681
  • 9
  • 30
  • 49
  • you could turn these notices off http://stackoverflow.com/questions/2867057/how-do-i-turn-off-php-notices BUT I suggest keeping them on for now. – Waygood Apr 24 '13 at 15:43
  • @Waygood So this is a non fatal error then ? Mind explaining what caused it though ? – Kalec Apr 24 '13 at 15:45
  • 1
    chandresh_cool already answered it. The explanation is that you are trying to access an element of $_POST that hasn't been created yet. (it is AFTER you submit the form). And BTW this is not MVC. – Waygood Apr 24 '13 at 15:46
  • Possible duplicate of [PHP: “Notice: Undefined variable” and “Notice: Undefined index”](http://stackoverflow.com/q/4261133/1409082) – Jocelyn Apr 24 '13 at 15:50

2 Answers2

3

Use:

if (isset($_POST['projectSubmit'])) { /*...*/ }

This checks if the parameter is sent. (here: when the form was submitted)

bwoebi
  • 23,637
  • 5
  • 58
  • 79
2

Use this

if (isset($_POST['projectSubmit']) && $_POST['projectSubmit'] == "Submit") 
bwoebi
  • 23,637
  • 5
  • 58
  • 79
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
  • Do you have any idea what exactly was wrong with mine ? I don't wanna follow tutorials from a site with bad code. – Kalec Apr 24 '13 at 15:45
  • 2
    You were directly checking the value of $_POST['projectSubmit'] you shoud first check is that post variable is available or not using isset because POST value only comes when you submit the form using button. – chandresh_cool Apr 24 '13 at 15:48