你好我寫一個基於文本的冒險中,你從一組選項中選擇擺弄周圍,我得到我的JS後進行功能工作並作出反應每當我按下然而一個按鈕獲得在這種情況發生時一直沒有去太多了。我一個div來加載和我已經在這個不確定的指數錯誤擱淺每當我試圖呼應從陣列作爲一個選項我有線索爲什麼這將是不明確的,如果有人知道爲什麼造成錯誤,以及如何解決它,並在未來避免它,我會非常感激,你真的〜雷德福德四獲得一個未定義的索引錯誤,不知道爲什麼完全失去了對原因和不能似乎找到任何其他的解決方案
所以我有這個作爲我的網頁:
<!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,即echo $輸出[$ q]
數組鍵必須是字符串或整數。你不能使用浮點值作爲鍵。如果你做'的var_dump($結果)',你會發現你'1.1'得到截斷只是'1'。 –
我可以使用right_road作爲字符串嗎? –
你可以使用任何你想要的字符串。如果你做了'$結果[「1.1」]'(注意引號),然後你的代碼會工作。 –