更新div值 - JQuery Toastmessage-plugin

时间:2012-06-24 16:17:34

标签: php jquery

我有一个php页面,重定向(如果成功)到另一个php页面,如下所示:

header('Location: completed.php?message=Successful operation. Data inserted for user: ' . $login->get_username() . ' . See home page for infos.');

现在在completed.php中,我有一个div,它回应收到的消息:

<div id="msg">
<?php echo $_GET['message']; ?>
</div>

我正在使用'Toastmessage-plugin':here..,但我无法在Toastmessage的文本中显示$ _GET。 我正在使用此代码:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script src="js/jquery.toastmessage.js" type="text/javascript"></script>
<link href="styles/jquery.toastmessage.css" rel="stylesheet" type="text/css" />

...

<script type="text/javascript">
function showStickyNoticeToast() {
        $().toastmessage('showToast', {
            text: $('#msg').val(),
            sticky: true,
            position: 'bottom-left',
            type: 'notice',
            closeText: '',
            close: function () { console.log("toast is closed ..."); }
        });
    }
</script>

我搜索了很多,也搜索了DOM事件和其他事件,但我不知道采取了什么步骤......也许当加载JQ函数时,div'msg'还没有收到GET数据...... ?那我怎么更新他的价值呢?

我读过的页面: thisthis。 我也尝试过ajax,但我对那种语言的经验不足......

请帮忙...... :)谢谢

编辑@charlietfl

Ajax:我做了很多尝试......我再也找不到代码了! 基本上我用这段代码调用一个php文件(把它作为伪代码):

<script type="text/javascript>
<!--
$('#choose').change(function(event) {
    $.get('messages.php', { selected: $('#msg').val() },
        function(data) {
            $('#update').html(data);   // other div wher store data
        }
    );            
}); 
</script>

...

<?php

$alfa = isset($_GET['message']) ? $_GET['message'] : 'nothing';
echo $alfa;
?>

3 个答案:

答案 0 :(得分:1)

尝试使用urlencode()传递消息,如:

$msg = "Successful operation etc. etc.";
header("Location: completed.php?message="+urlencode($msg));

答案 1 :(得分:0)

知道了:您使用$("#msg").val()DIV标签没有“值”。

使用$("#msg").text()(用于纯文本)

$("#msg").html()

还要在代码中包含一些HTML(非常确定您需要第一个选项)。

仅将.val()<input>等“有价值”标签一起使用(也适用于<textarea>)。

另请参阅:http://api.jquery.com/val/

答案 2 :(得分:-1)

您的问题很可能是您尝试在ajax完成之前调用消息插件,因为第一个&#39; A&#39;在ajax中代表异步。

为了允许ajax完成所需的时间,你需要在ajax的成功回调中调用你的消息插件。

message参数添加到函数

function showStickyNoticeToast( message ) {
        $().toastmessage('showToast', {
            text: message,
            sticky: true,
            position: 'bottom-left',
            type: 'notice',
            closeText: '',
            close: function () { console.log("toast is closed ..."); }
        });
    }



$.post( url , dataToserver, function(data){         
      showStickyNoticeToast( $(data).text())
      $('#update').html(data); 

})