简体   繁体   English

Ajax:成功将数据保存到数据库后,在另一页上显示通知

[英]Ajax: displaying notification on another page, after successfully saved data to database

I have standed out for awhile now with a thing i want to do. 我现在已经想做一件事情而脱颖而出。 Thing i want to do is to display a message(confirmation, "You have earned 2 points") at the top (message like stackoverflow). 我想做的是在顶部显示一条消息(确认,“您已经获得2分”)(类似stackoverflow的消息)。

Ive did this storing a confirmation message using session variable, and then at index.php there is a ajax script that loads session.php each 1 second, for checking if there's some message to display. 我已经使用会话变量存储了确认消息,然后在index.php处有一个ajax脚本,该脚本每1秒加载一次session.php,以检查是否有消息要显示。

BUT i wonder, cant this be done in another way? 但是我想知道,这不能用另一种方式完成吗? I mean, does it need to check each 1 second all the time 我的意思是,是否需要一直检查每一秒

My site is a design on index.php, and then you are browsing the rest inside a frame. 我的网站是index.php上的设计,然后您在框架中浏览其余内容。

Anyway, i already got a solution for this as you see, i think it may request too much to the server. 无论如何,如您所见,我已经为此找到了解决方案,我认为这可能会对服务器提出过多要求。

So i wonder, cant this be done in another way? 所以我想知道,这不能用另一种方式吗? I mean, does it need to check each 1 second all the time? 我的意思是,是否需要一直检查每一秒钟?

Isnt it possible to, if you have inserted a comment correctly, and stored session variable called user_message, then tell index.php to show the #box. 如果您正确插入了注释并存储了名为user_message的会话变量,那么是否有可能,然后告诉index.php显示#box。

My comment "form" is on show.php, which just contains a normal text area and a submit button. 我的评论“表单”在show.php上,其中仅包含一个正常的文本区域和一个提交按钮。 Now it stores the message through a JS script that passes it all to a string, to insert.php which inserts with normal mysql query to the database, and stores a session variable. 现在,它通过一个JS脚本存储消息,该消息将所有消息传递给一个字符串,插入到insert.php中,该消息将使用普通的mysql查询插入数据库,并存储一个会话变量。 (source code below) (下面的源代码)

Here's some coding: session.php: 这是一些编码:session.php:

<?php 
session_start();
if(isset($_SESSION['user_message'])) {
echo 1; 
}
?>

div box in index.php: index.php中的div框:

<div id='box' onclick="closeNotice()" style="display: none">
Hey, <b><? echo $pusername; ?></b> - <? echo $_SESSION["user_message"]; ?> 
<a href="#" class="close-notify" onclick="closeNotice()">X</a>
</div> 
<?php
$_SESSION["user_message"] = null; 
?>

ajax script that refresh each 1 second(that i am using now): 每1秒钟刷新一次的ajax脚本(我现在正在使用):

function checkSession(){
    $.ajax({url: "session.php", success: function(data){
         if( data == 1){
             $('#box').show();
         }else{
             $('#box').hide();
         }
    }});
}
setInterval('checkSession()',1000);

Script that passes string to insert.php which inserts the comment to the database: 将字符串传递给insert.php的脚本,该脚本将注释插入数据库:

var nocache = 0;
function insert() {
document.getElementById('insert_response').innerHTML = "Please Wait .. "
var fID= encodeURI(document.getElementById('fID').value);
var kommentar= encodeURI(document.getElementById('kommentar').value);
nocache = Math.random();
http.open('get', 'insert.php?fID='+fID+'&kommentar=' +kommentar+'&nocache = '+nocache);
http.onreadystatechange = insertReply;
http.send(null);
}
 function insertReply() {
 if(http.readyState == 4){
 var response = http.responseText;
 document.getElementById('insert_response').innerHTML = ''+response;
 }
} 

The form on show.php: show.php上的表格:

<div id="insert_response">
</div>
<form action="javascript:insert()" method="post">
<textarea id="kommentar" name="kommentar"></textarea><br />
<input type="hidden" name="fID" id="fID" value="<? echo $_GET["id"]; ?>" />
<input type="submit" name="Submit" value="Sæt ind!"/> 
</form>

Why not just set dataType to "script" with your $.ajax call-- and have the php script that the $.ajax method calls generate the appropriate javascript to insert/remove/show/hide/modify items on your page accordingly? 为什么不通过$ .ajax调用将dataType设置为“ script”,并让$ .ajax方法调用的php脚本生成相应的javascript,以相应地插入/删除/显示/隐藏/修改页面上的项目?

I think you're over-thinking this problem, use of sessions in this case i think is unnecessary as well... and I would if at all possible try to avoid that. 我认为您对这个问题的想法过高,在这种情况下使用会话我也认为是不必要的...并且我将尽可能避免这种情况。

Look here-- http://api.jquery.com/jQuery.ajax/ -- at the parameter "dataType" for the specific parameter "script" and I think you'll find it helpful in what you're trying to accomplish. 看这里-http : //api.jquery.com/jQuery.ajax/-在参数“ dataType”处的特定参数“ script”,我想您会发现它对您要完成的工作很有帮助。

There is no needs for sessions unless you want the message to stay across multiple pages. 除非您希望消息停留在多个页面上,否则不需要会话。

If your trying to send the user to another page after pressing submit store the ajax response into a hidden input and use $_POST to retrieve it. 如果您尝试在按提交后将用户发送到另一个页面,则将ajax响应存储到一个隐藏的输入中,并使用$ _POST进行检索。

If same page, just save the ajax response to box and call box.show() to display. 如果页面相同,只需将ajax响应保存到box并调用box.show()进行显示。

Good? 好?

I think that you'll need comet to get rid of the requests. 我认为您将需要彗星摆脱请求。 However, I haven't used it myself and I don't know much about it so I can't really help you with that. 但是,我自己并没有使用过它,对此我也不了解,所以我无法真正为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM