0

Hi im writing a text based adventure in which you choose from a set of choices to proceed after fiddling around i got my js function to work and react whenever i press a button however getting to load in another div when that happens hasn't gone too well for me and i've run aground on this undefined index error whenever i try to echo an option from a array as i have clue why it would be undefined if someone knows why the error is caused and how to fix it and avoid it in the future i would be very grateful, Yours truly ~Rendord IV

So i have this as my page:

<!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>

and this as my 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];

    ?> 

the whole middle_content division on the first page is supposed to change everytime the buttons are pressed changing the buttons and the text so that you further in the story however when i press the first button the page seems to understand that the middle content is going to change but then outputs a undefined index error 1.1 on line 9 which is the echo $outcome[$q]

1 Answers1

0

Array keys must be strings or integers. floats will simply get truncated down to an 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"
}

Note how two array entries were created, even though kinda/sorta/notreally you're using the "same" value for both.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • i see i had no idea there was a distance this has helped me out and i hope i can resort to my own wits when running into a wall that i didnt know was there in the future – Rendord IV May 14 '15 at 14:47
  • `var_dump` and `print_r` are your friends... never leave home without them. – Marc B May 14 '15 at 14:48