Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP PHP Warning: Undefined array key

cbreemer

Bronze Coder
Hey folks,
I hope someone can help explaining two PHP warnings.
My calling code is
JavaScript:
xhr.open('POST', "manfac.php", false);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(`func=facturen&name=*`);

My PHP file manfac.php starts with these lines:
PHP:
 <?php

$func = $_POST["func"];
$name = $_POST["name"];
var_dump($_POST);

In the debugger network tab I see these warnings:
Code:
PHP Warning:  Undefined array key "func" in C:\Users\cbree\OneDrive\manfac.php on line 3
PHP Warning:  Undefined array key "name" in C:\Users\cbree\OneDrive\manfac.php on line 4

The var_dump command produces this info in the xhr return data:
Code:
 array(2) {
  ["func"]=>
  string(8) "facturen"
  ["name"]=>
  string(1) "*"
}
so it seems to me the two parameters func and name are correctly passed with their values facturen and *.
And the PHP code, using these variables $func and $name works as expected.

So why the warnings ? I must be missing something obvious.
 
Solution
I finally realized what was happening here. The Debugger Network tab showed test,php as a link, and being the gullible pensioner I am, I clicked on it hoping to see some useful stuff. All that did was excute the php a second time, this time without parameters. And then I wonder why that shows the error... Derp.
Looking at this line -> xhr.send(func=facturen&name=*);
It's hard to tell if this is a single quote or a tilde mark. What happens if you use the double quote?>
No tildes here señor 🤓
And the quotes are backticks, carried over from the original code which had variable names in there.
It makes no difference if I replace them by normal quotes, be it single or double.
 
I finally realized what was happening here. The Debugger Network tab showed test,php as a link, and being the gullible pensioner I am, I clicked on it hoping to see some useful stuff. All that did was excute the php a second time, this time without parameters. And then I wonder why that shows the error... Derp.
 
Solution

New Threads

Buy us a coffee!

Back
Top Bottom