提问人:timestopMachinist 提问时间:11/12/2023 最后编辑:timestopMachinist 更新时间:11/12/2023 访问量:38
为什么我不能将我的 str 变量从 JavaScript 传递到 PHP 中的 $q 变量?[关闭]
Why can't I pass my str variable from JavaScript to my $q variable in PHP? [closed]
问:
做编码作业,不知道我在这里还需要做什么。我收到一个错误,上面写着“警告:第 63 行 C:\xampp\htdocs\title_hints.php 中的未定义数组键”q”。
<!--
Name: x
Date: x
Class: x
Assignment: x
Filename: title_hints.php
-->
<!DOCTYPE html> <!--Proper html heading-->
<html> <!--open html element-->
<head>
<script language="JavaScript" type="text/javascript">
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innherHTML = "";
return;
}
if (window.XMLHttpRequest) {
var xmlhttp = new XMLHttpRequest();
} else {
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementByID("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "title_hints.php?q=" + str, true);
xmlhttp.send();
}
</script>
<h2>Movie Title Search</h2> <!--header-->
</head>
<?php //open php element
$movieList[] = "Alice in Wonderland";
$movieList[] = "The Raven";
$movieList[] = "Introduction to Javascript";
$movieList[] = "The Restaurant at the End of the Universe";
$movieList[] = "Cinderella";
$movieList[] = "Prelude to Programming";
$movieList[] = "The Seeker";
$movieList[] = "The Borrowers";
$movieList[] = "Hunger Games";
$movieList[] = "The Girl with the Dragon Tattoo";
$movieList[] = "The Diary of Anne Frank";
$movieList[] = "Murders in the Rue Morgue";
$movieList[] = "Little Women";
$movieList[] = "Little Men";
$movieList[] = "The Scarlet Letter";
$movieList[] = "Catch 22";
$movieList[] = "The Grapes of Wrath";
$movieList[] = "The Tale of Benjamin Bunny";
$movieList[] = "A Tale of Two Cities";
$movieList[] = "The Secret Garden";
$movieList[] = "The Little Prince";
$q = $_GET['q'];
echo $q;
//$hint = ""; //????
if (strlen($q) > 0) {
$hint = "";
for ($i = 0; $i < count($movieList); $i++) {
if (strtolower($q) == strtolower(substr($movieList[$i], 0, strlen($q)))) {
if ($hint == "") {
$hint = $movieList[$i];
} else {
$hint = $hint . ";" . $movieList[$i];
}
}
}
}
if ($hint == "") {
$response = "no matches";
} else {
$response = $hint;
}
echo $response;
//if (isset($_POST["movieQuery"]) && srtlen($_POST["movieQuery"]) >0) {
?> <!--close php element-->
<br>
<br> <!--a couple line breaks to separate php text output from body element-->
<body> <!--open body element-->
<form>
<!--open form element-->
Enter query:<br /> <!--describe following textbox's purpose-->
<input type="text" onkeyup="showHint(this.value)" size="20" /><br /> <!--user input box-->
</form> <!--close form element-->
<h3>Results: <span id="txtHint"></span></h3>
</body> <!--close body element-->
</html> <!--close html element-->
据我所知,JavaScript 应该将变量传递给带有 .open 的 url,然后我使用 $_GET 在 php 中接收它?最终结果应该是一个页面,允许用户在一个框中输入,并在用户输入时显示匹配的电影标题(我没有选择电影)。我基本上已经尽可能地从我的教科书中复制了大部分代码,但评论说我的教科书不可靠。教授问道,但似乎懒得帮忙。还尝试使用(大学提供的)在线导师,但一个小时后一无所获。任何帮助都值得赞赏,我不知道还能去哪里。任何帮助都非常感谢。
答: 暂无答案
评论
q
$_GET
$q = $_GET['q'] ?? ''
$movieList = ['title1', 'title2', 'title3',];
<!DOCTYPE html>
if($_SERVER['REQUEST_METHOD'] === "GET"){ ... }