I was wondering if you could help me with the following:
I am passing some info开发者_StackOverflow社区rmation 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
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']
.
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
精彩评论