Jump to content

Warning: Undefined array key "param1" in D:\web\_BH\test.php on line 2


cbreemer
Go to solution Solved by cbreemer,

Recommended Posts

Hi folks,

I'm new here and while I can get by in PHP I am far from fluent. I hope someone can explain a warning that has been nagging me for a wile now.

I'm doing an Ajax post from Javascript to a PHP script, passing two variables like this (some code omitted for brevity)

    var xhr = new XMLHttpRequest();
    xhr.open('POST', "test.php", false);
    xhr.send("param1=foo&param2=bar");

The PHP code picks up these values and echo them back:

<?php
$p1 = $_POST["param1"];
$p2 = $_POST["param2"];
echo "param1 = " . $p1 . "\n";
echo "param2 = " . $p2 . "\n";
?>

This produces the following two warnings:

Warning: Undefined array key "param1" in D:\web\_BH\test.php on line 2
Warning: Undefined array key "param2" in D:\web\_BH\test.php on line 3

Yet, it works ! The output of the echo statements is duly passed back in the request's response test, correctly showing the values foo and bar. So why the warnings ? Am I missing the bleeding obvious ? This is driving me nuts... it's not a problem but I so hate warnings and errors.
Thanks for any ideas 😊

 

Link to comment
Share on other sites

5 hours ago, mac_gyver said:

your full code is probably doing something like making two requests, one with out data and one with. you would need to post all your code to get specific help with what is wrong with it.

Thanks for the reply. Unless I grossly misunderstand the XMLHttpRequest mechanism, I don't think my code is making two requests. I see only one in the debugger as expected (see image). This shows, I believe, that everything is working correctly.
My full HTML/JS code:

<!DOCTYPE html>
<html lang="en">
<head>
<script>
function postit()
{
    var xhr = new XMLHttpRequest();
    xhr.open('POST', "test.php", false);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function ()
    {
        if (xhr.readyState === 4)
        {
            ret = xhr.responseText;
            if (xhr.status != 200)
            {
                ret = `\n${xhr.status} ${xhr.statusText}`;
                alert(ret);
                return;
            }
        }
    }

    xhr.send("param1=foo&param2=bar");
    result.value = ret;
}

</script>
</head>

<body onload="postit()">
<textarea id="result"></textarea>
</body>
</html>

and the full PHP code:

<?php
$p1 = $_POST["param1"];
$p2 = $_POST["param2"];
echo "param1 = " . $p1 . "\n";
echo "param2 = " . $p2 . "\n";
?>

 

request.jpg

Link to comment
Share on other sites

3 hours ago, ginerjm said:

It means that your form is missing 2 fields named 'param1' and 'param2'.

Haha yes, I understand what the message is trying to tell me. But it's just not true ! My request data is

param1=foo&param2=bar

so that should be ok, no ? Plus, it actually works (see my reply to mac_gyver). But for shits and giggles, I will try with a HTML form instead of Ajax.

Thanks for the tip about these error reporting statements. I'll make sure to include these from now on. It did not make any difference in this case though,

Link to comment
Share on other sites

  • Solution

Well hehe, I got it at last. And boy, this is embarrassing.
First. inspired by ginerjm's first reply, I dispensed with the XMLHttpRequest and used a simple form with a POST action, and I saw the same thing again. Then I realized that mac_gyver's question 

Quote

where exactly are you getting/seeing the php warnings?

was exactly the right thing to ask. Suddenly everything fell into place.

See images. The first shows that the test has worked ok. The input data is echoed back to the form via PHP. But in the network tab I see test.php which appears to be a link. Thinking this would give some interesting information, I blithely click on it an get the screen with the error - image 2. Yep, that's what I do, when I see a link I click on it 🙄 I had long noticed the extra browser tab but somehow never realized this means that test.php has been executed a second time, this time without parameters. Of course that would produce these errors. Derp.

So that's all it was - seeing errors when there were none. I feel like a right idiot now 😲
Thanks both of you for the reactions. They were really helpful. 

image 1.jpg

image 2.jpg

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.