提问人:Dan B. 提问时间:8/2/2017 最后编辑:Dan B. 更新时间:8/2/2017 访问量:1346
获取通知:未定义的索引:....在第 4 行 [复制]
getting Notice: Undefined index: text in .... at line 4 [duplicate]
问:
这个问题在这里已经有答案了:
使用 PHP 的“注意:未定义的变量”、“注意:未定义的索引”、“警告:未定义的数组键”和“注意:未定义的偏移量” (29 个答案)
何时使用 POST,何时使用 GET? (27 个答案)
6年前关闭。
4:$textik = $_POST['text'];
我得到
通知:未定义的索引:第 4 行 path/to/script 中的文本。
为什么?
网址是
感谢您的回答http://somesite.domain/path/to/script?text=something
编辑: path/to/script 中的完整脚本是
<?php
session_start();
if(isset($_SESSION['name'])){
$textik = $_POST['text'];
date_default_timezone_set(date_default_timezone_get());
$time = date('h:i', time());
$fp = fopen("log.html", 'a');
fwrite($fp, "<li class='other'>
<div class='msg'>
<div class='user'>".$_SESSION['name']."</div>
<p>".stripslashes(htmlspecialchars($textik))."</p>
<time>".$time."</time>
</div>
</li>");
fclose($fp);
}
?>
在 path/to/anotherscript 中是
<?php
session_start ();
function loginForm() {
echo '
<div id="loginform">
<form action="index.php" method="post">
<p>Please enter your name to continue:</p>
<label for="name">Name:</label>
<input type="text" name="name" id="name" />
<input type="submit" name="enter" id="enter" value="Enter" />
</form>
</div>
';
}
if (isset ( $_POST ['enter'] )) {
if ($_POST ['name'] != "") {
$_SESSION ['name'] = stripslashes ( htmlspecialchars ( $_POST ['name'] ) );
$fp = fopen ( "log.html", 'a' );
date_default_timezone_set(date_default_timezone_get());
$time = date('h:i', time());
fwrite ( $fp, "<p class='notification'> ". $_SESSION ['name'] . " left the group <time>". $time."</time></p>" );
fclose ( $fp );
} else {
echo '<span class="error">Please type in a name</span>';
}
}
if (isset ( $_GET ['logout'] )) {
// Simple exit message
$fp = fopen ( "log.html", 'a' );
fwrite ( $fp, "<div class='msgln'><i>User " . $_SESSION ['name'] . " has left the chat session.</i><br></div>" );
fclose ( $fp );
session_destroy ();
header ( "Location: index.php" ); // Redirect the user
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel='stylesheet' type='text/css' href='new.css'>
<title>AZsites chat</title>
</head>
<body>
<?php
if (! isset ( $_SESSION ['name'] )) {
loginForm ();
} else {
?>
<div id="wrapper">
<ol class="chat">
<div class="menu">
<a href="#" class="back"><i class="fa fa-angle-left"></i> <img src="https://i.imgur.com/G4EjwqQ.jpg" draggable="false"/></a>
<div class="name">AZsites chat</div>
<div class="members"><b><?php echo $_SESSION['name']; ?></b> a dalsi</div>
</div><?php
if (file_exists ( "log.html" ) && filesize ( "log.html" ) > 0) {
$handle = fopen ( "log.html", "r" );
$contents = fread ( $handle, filesize ( "log.html" ) );
fclose ( $handle );
echo $contents;
}
?></ol></div>
<div class="typezone">
<form name="message" action='' method='post'><textarea name="text" id="usermsg" size="63"type="text" placeholder="Napis neco"></textarea><input type="submit" class="send" value="odeslat"/></form>
<div class="emojis"></div></div>
</div>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function(){
});
//jQuery Document
$(document).ready(function(){
//If user wants to end session
$("#exit").click(function(){
var exit = confirm("Are you sure you want to end the session?");
if(exit==true){window.location = 'index.php?logout=true';}
});
});
//If user submits the form
$("#submitmsg").click(function(){
var clientmsg = $("#usermsg").val();
$.post("post.php", {text: clientmsg});
$("#usermsg").attr("value", "");
loadLog;
return false;
});
function loadLog(){
var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height before the request
$.ajax({
url: "log.html",
cache: false,
success: function(html){
$("#chatbox").html(html); //Insert chat log into the #chatbox div
//Auto-scroll
var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20; //Scroll height after the request
if(newscrollHeight > oldscrollHeight){
$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
}
},
});
}
setInterval (loadLog, 500);
</script>
<?php
}
?>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
</script>
</body>
</html>
所以我需要发布,因为它在后台运行,普通用户看不到它,但在这里它有一个编辑可以看到它,但我不知道如何将其编辑到正常状态..... 请帮帮我... 谢谢!
答:
0赞
Scott
8/2/2017
#1
您需要使用 ,而不是GET
POST
$textik = $_GET['text'];
请阅读这篇 W3Schools 文章以进一步澄清。
1赞
node_modules
8/2/2017
#2
您正在使用而不是 。您的 URL 是 http://somesite.domain/path/to/script?text=something,并且是 GET 参数。$_POST
$_GET
text
所以你需要改变
$textik = $_POST['text'];
自
$textik = $_GET['text'];
评论
1赞
Dan B.
8/3/2017
没错,但我编辑了它并且它比它工作,但它更像以前,它有 POST,而不是 GET。但是谢谢,现在我知道该怎么做了。
评论