1

I have read so many pages and am stuck on this for the past three hours now because it just won't work.

I keep getting Notice: Undefined index: firstname

here is the bulk of the segment that isn't working:

$errMsg = "";

function sanitise($data){
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

if (isset($_POST["firstname"]))
{
    $firstname = $_POST["firstname"];
    $firstname = sanitise($firstname);

    if (!preg_match("/^[A-Za-z \-]+$/",$firstname))
    {
        $errMsg .= "First name must contain only letters or hyphens.<br/>";
    }

    if (strlen($firstname) > 40)
    {
        $errMsg .= "First name cannot be over 40 characters long.<br/>";
    }
} else {
    $errMsg .= "First name cannot be empty.<br/>";
    $firstname = "";
}


    if ($errMsg != "")
    {
    header("Location: fix_order.php?firstname=$firstname");
    }

this is the code on fix_order.php where I want to access the variables.

$firstname = $_GET["firstname"];
echo "<p>firstname is $firstname .</p>";

I have tested the $firstname on the first page and it echo's the values just fine.

Noob
  • 319
  • 1
  • 3
  • 16

2 Answers2

1

change your code to

$firstname = $_GET["errMsg"];
echo "<p>firstname is $firstname .</p>";

You can't access $firstname as this is the value not the key. errMsg is the key you should use.

Junius L
  • 15,881
  • 6
  • 52
  • 96
  • Notice: Undefined index: errMsg in /home/students/accounts/s6883826/cos10011/www/htdocs/assign3/fix_order.php on line 24 – Noob May 19 '17 at 14:14
  • sorry mate, i had it as errMsg before to test something. I changed the key back to firstname and I still run into the same errors. Notice: Undefined index: firstname in /home/students/accounts/s6883826/cos10011/www/htdocs/assign3/fix_order.php on line 24 – Noob May 19 '17 at 14:17
  • the code you pasted is not complete right?, do you have any other `header` location? – Junius L May 19 '17 at 14:18
  • i pasted it there... but can you see it? – Noob May 19 '17 at 14:24
  • i added it. it isn't complete yet. matter of fact most of it will be scrapped as I am recreating a new page there. below the 3rd php script, will be changed... – Noob May 19 '17 at 14:28
  • I think you are calling `$_GET['firstaname']` in either `main.inc` or `menu.inc` as I have commented them out and it works, no errors. – Junius L May 19 '17 at 14:32
  • I just posted the answer from one of the comments from the actual question. Thank you anyway for your time! I really appreciate it! especially after 4 hours of pulling my hair out... was a simple full stop... – Noob May 19 '17 at 14:34
0

Looks fine to me, it should work.

To get rid of the notice, you need to define variable by using isset()

$firstname = isset($_GET['firstname']) ? $_GET['firstname'] : '';

OR

if(isset($_GET['firstname'])) {
    $firstname = $_GET['firstname'];
} else {
    $firstname = '';
}
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
devofash
  • 328
  • 2
  • 13