我有一个包含textarea的表单,用于输入文本。表单还包含一个提交按钮。按下提交按钮后,它会将textarea中的文本发布到我的php文档中。在我的php文档中,文本被添加到数据库中。一旦将它添加到数据库中,我希望它回显一个响应,告诉用户它已成功将文本添加到数据库。 但是,如果我让它回应主页的响应,那么就没有声明它显示回显的消息。有没有人知道我应该做什么才能使这个工作?非常感谢。
通常情况下,我不会直接使用表单中的帖子,我会使用ajax,然后在段落或其他内容中显示数据返回,但是由于表单正在发布它自己,我不知道在哪里然后声明响应应该出现在哪里。
bellow显示我的html表单代码并显示发布到php文件的操作。
<div id="userban2"><form id="bannable" action="/onlineusers.php" method="post"><p> Type username to ban bellow:</p>
<textarea name="banned" id="banned" maxlength="255"></textarea><br/>
<input type="submit" value="Send" class="extrabuttons" onclick="return false; preventDefault();">
<div id="cancelban" class="extrabuttons"><p> cancel</p></div>
</form>
然而,当我在我的php文件中写道时....
echo "the information has been added to the database successfully";
它可能会发回回来但是它没有被声明显示在任何地方我如何更改它以使其在我的表单中显示响应?
请求从我的php返回
if(isset($_POST["banned"])){
$ban_name = $_POST["banned"];
bannedd($ban_name);
}
function bannedd($ban_name) {
$query1 = mysql_query("INSERT INTO banned_users (username,firstname,lastname,email,password,ip_address,sign_up_date,last_logged_in,about,sta rr,userpref) VALUES('$usernameb','$fnameb','$lnameb','$emailb','$passwordb','$ip_addressb','$sign_up_date b','$last_logged_inb','$aboutb','$starrb','$userprefb')") or die("Could not insert your informaion");
echo "This user has successfully been banned";
}
表单发布了表单中写的内容,因为它具有发布到我的php的操作和方法。但是,如果我有任何回报,我不知道如何声明返回的信息应该显示在哪里(回显的消息)。
答案 0 :(得分:2)
如果我理解正确,您的表单位于某个index.php
文件中并将数据发送到其他文件 - onlineusers.php
,并且您希望在原始页面?
如果是这种情况,我能想到的最简单的方法是使用URL参数重定向回原始页面,而不是回显。
在onlineusers.php
:
<?php
// insert text into DB ...
header("Location: index.php?result=ok");
?>
这会将浏览器重定向回带有表单的原始页面。在那里检查是否设置了状态变量:
<html>
<head></head>
<body>
<?php if(isset($_GET["result"]) && $_GET["result"]=="ok") { ?>
<p>The information has been added to the database successfully</p>
<?php } ?>
<form> ... </form>
</body>
</html>
正如您可能看到的那样,您可以设置其他结果,例如“错误”。
如果你不喜欢你的URL中的额外字符串,那么在处理onlineusers.php
中的表单并返回原始页面后创建一个cookie,检查是否已经设置了这样的cookie。如果您需要更多细节,请告诉我。如果你问的是完全不同的东西,那么,不要紧。:)
答案 1 :(得分:0)
您的表单已提交至/onlineusers.php
这是您想要添加echo语句的地方。
答案 2 :(得分:0)
如果您需要同一页面上的信息,您在技术上会返回同一页面,表单操作为$ _SERVER [&#39; PHP_SELF&#39;]。
<form id="bannable" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
然后,您可以在加载文档之前放入条件语句,并包含PHP脚本。
<?php
$testVar = false;
$msg = '';
if($_POST) {
include '/onlineusers.php';
//... do something e.g post to database and return true.
}
if($testVar) {
$msg = 'Successful writing to DB!';
} ?>
<html>
<body>
<?php echo $msg; ?>
</body>
</html>
这将检查您是否有任何发布数据,如果有,则包含您指定的脚本。如果写入DB成功,可以将$ testVar设置为true,然后在HTML中返回$ msg。