得到一个未定义的索引错误,不知道为什么完全不知道为什么,似乎找不到任何其他解决方案


Getting an undefined index error and have no clue why totally lost on why and cant seem to find any other solutions

嗨,我正在写一篇基于文本的冒险文章,在这篇文章中,你可以从一组选项中进行选择。我让js函数工作起来,每当我按下按钮时都会做出反应。然而,当这种情况发生时,加载到另一个div对我来说并不太好,每当我试图从数组中回显选项时,我都会遇到这个未定义的索引错误,因为我不知道为什么如果有人知道为什么会导致错误,以及如何在未来修复和避免错误,那将是不确定的。我将非常感激,你真的~ Rendord IV

所以我有这个作为我的页面:

<!DOCTYPE html>
<HTML>
  <head>
    <script type="text/javascript">
function choice(str) {
            if (str == "") {
                return;
            } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("middle_content").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "choice_sets.php?q=" + str, true);
        xmlhttp.send();
    }
}
    </script>
    <link rel="stylesheet" type="text/css" href="mystyle.css">
  </head>
  <body>
    <div id="middle">
        <div id="middle_content">         
          <form action="choice_sets.php" method="POST">
          <p>You awaken, your throat is parch and your lips are dry. you have no idea how you got here
            and how long you've been here but one thing is sure you need both water and food.
            you follow the path and soon enough you see that the road splits in two at a very big tree.<br><br>
          <input type="radio" name="choice_1" value="1.1" onclick="choice(this.value)">Follow the path to the right<br>
          <input type="radio" name="choice_1" value="1.2" onclick="choice(this.value)">Follow the path to the left<br>
          <input type="radio" name="choice_1" value="1.3" onclick="choice(this.value)">Walk closer to the tree<br>
          </form>
        </div>
    </div>
  </body>
</HTML>

这是我的php

<?php
  $outcome[1.1] = "<p>After walking the right road for what seems to be an  
  hour you sight a small hamlet in the distance. The thought of a warm bed 
  and food and water appeal to you but you are not sure, What do you do </p>";
$q = $_REQUEST["q"];
    echo $outcome[$q];
    ?> 

第一页上的整个中间内容划分应该在每次按下按钮时都会改变——改变按钮和文本,这样你就可以进一步了解故事了。然而,当我按下第一个按钮时,页面似乎理解了中间内容将要改变,但随后在第9行输出了一个未定义的索引错误1.1,这就是echo$output[$q]

数组键必须是字符串或整数。浮点值将被简单地截断为int:

php > $arr[1.1] = 'foo';    // 1.1 as float value
php > $arr['1.1'] = 'bar';  // 1.1 as string
php > var_dump($arr);
array(2) {
  [1]=>
  string(3) "foo"
  ["1.1"]=>
  string(3) "bar"
}

请注意两个数组条目是如何创建的,尽管这两个条目实际上都使用了"相同"的值。