提问人:Rendord IV 提问时间:5/14/2015 更新时间:5/14/2015 访问量:179
得到一个未定义的索引错误,不知道为什么完全迷失了原因,似乎找不到任何其他解决方案
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];
?>
每次按下按钮时,第一页上的整个middle_content划分都应该发生变化,更改按钮和文本,以便您在故事中进一步发展,但是当我按下第一个按钮时,页面似乎明白中间内容将发生变化,但随后在第 9 行输出未定义的索引错误 1.1,这是回声$outcome[$q]
答:
0赞
Marc B
5/14/2015
#1
数组键必须是字符串或整数。floats 将被简单地截断为 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"
}
请注意两个数组条目是如何创建的,即使 kinda/sorta/notreally 你对这两个数组条目都使用了“相同”的值。
评论
0赞
Rendord IV
5/14/2015
我看到我不知道有一段距离,这对我有帮助,我希望当遇到一堵我不知道将来会存在的墙时,我可以诉诸自己的智慧
0赞
Marc B
5/14/2015
var_dump
并且是你的朋友......永远不要离开家,没有他们。print_r
评论
var_dump($outcome)
1.1
1
$outcome['1.1']