tags:

views:

94

answers:

2

Hello guys, I was wondering if you could help me with the following:

I am passing some information filled in a form to the next page to show. I am using $_POST method. Although the information is shown in the browser when checking the code is shows an Notice: undefined index error and there is no trace of the value passed where it should be (in code). I think this is related to the error above.

The way I am calling it is for ex:

$name = $_POST['name'];

Also I am trying to get the same information in some hidden fields in order to store them (after viewing the page) in a database but here as well there is no value passed. The value in the hidden is empty.

Sorry if this seems a silly thing...but staked and don't know what I am doing wrong. Really appreciate any help. Many thanks Francesco

+1  A: 

For debug purposes you can try:

print_r($_POST);

It will show all the variables in a post array. Look if your variable is in there, if not - there is something wrong with your form.

Leonti

Leonti
Thank you Leonti. The variables are there. As said above the value are actually shown into the page thus they are passed.
francesco
Then they are being unset somewhere or the values you see on the page are not from the $_POST var.
Mike B
+5  A: 

In PHP you can not read-access variables that don’t exist. For non-existing normal variables like $foo or $bar you will get a Undefined variable notice, for non-existing array indices like $array['foo'] or $array['bar'] you will get a Undefined index notice and for non-existing object properties like $object->foo or $object->bar you will a Undefined property notice.

This is because in PHP variables must be declared before a read-access can be done. PHP has the function isset to test if a variable exists, array_key_exists for array indices and property_exists for object properties. Furthermore isset is some kind of universal tool to test the existence. (Note that the value null is equivalent to not existing.)

So in your case you should test if $_POST['name'] exists before trying to read it:

if (array_key_exists($_POST, 'name')) {
    $name = $_POST['name'];
}
// or
if (isset($_POST['name']) {
    $name = $_POST['name'];
}

But note that the existence of $name now depends on the existence of $_POST['name'].

Gumbo
Thank you Gumbo for your feedback, although I still not sure what I am doing wrong. I get a warning using isset. Does this mean that the values are wrongly posted?
francesco
@francesco: Whenever you want to read variables whose existence you are not sure of you need to test is with `isset`. Take a look at the examples I’ve posted where `$_POST['name']`’s existence is tested before I read its value. But now `$name` depends on `$_POST['name']` and you would need to test if that variable exists too whenever you want to read it. Or you declare `$name` with a default value (e.g. *null* or `""`) and overwrite it only if `$_POST['name']` exists.
Gumbo
Thank you Gumbo.
francesco