如何使用jquery.get()方法在会话变量中返回成功消息

时间:2010-04-30 12:46:05

标签: php jquery

我使用jQuery.get()从页面main.php中删除表格中的行。现在我想在删除行后显示成功消息,成功消息应该在会话变量($_session['suxesMsg']

  1. 如何在会话变量上设置成功消息并在特定范围内显示?
    1. 在jQuery中是否有任何其他方法可以显示5-10的消息 仅秒,然后消失?
    2. 这是我的代码

      main.php


       <?php  if($_SESSION['suxesMsg']!='') { ?>
         <span class="msg">
           <?php echo $_SESSION['suxesMsg'];unset($_SESSION['suxesMsg']);  } ?>
         </span>
      
        <table border="0" cellpadding="5" cellspacing="0" id="promotionTable">
         <tr>
            <td align="left"><img border='0' src='images/just.gif'/>First Promotion</td>
            <td align="center" >View Detail</td>
            <td align="center" id="deleteMe">
             <img src='images/delete.png' alt='Delete' width='14' height='14'id="45"/>
            </td>                
         </tr>
          <tr>
            <td align="left"><img border='0' src='images/just.gif'/>First Promotion</td>
            <td align="center" >View Detail</td>
            <td align="center" id="deleteMe">
             <img src='images/delete.png' alt='Delete' width='14' height='14' id="48"/>
            </td>                
         </tr>
         </table>
      
      
      
      <script type="text/javascript">        
          jQuery(document).ready(function(){      
              jQuery('#deleteMe img').click( function() {   
                  if(!confirm('Want to delete!')) return; 
              jQuery.get('deleteThis.php', {oid:this.id});
              jQuery(this).parent().parent().fadeTo(400, 0, function() {
                 jQuery(this).remove();
              });
          });
         </script>
      

      deleteThis.php


      if(isset($_GET[oid]))
          {
              $offerID=$_GET[oid];    
              $delsql="DELETE FROM some_table WHERE promotion_id=".$offerID;  
              $db->query($delsql);        
              $_SESSION['suxesMsg'] = "Promotion deleted sucessfully.";
          }
      

      感谢帮助我alwayz

1 个答案:

答案 0 :(得分:3)

我不确定为什么需要进入会话。查看代码,这意味着您需要在显示实际错误消息之前刷新页面,因为它是在最初生成页面时在PHP中完成的。

您是否可以直接在删除项目的请求中返回成功消息,然后使用javascript立即显示?您可以使用JSON编码成功标志和发回的字符串消息。例如:

//make post request to delete item
$.post'deleteThis.php', {oid:this.id}, function(data) {
    if (data.success) {
       $('.msg').html('Success: ' + data.msg);
    } else {
       $('.msg').html('Failed: ' + data.msg);
    }
}, "json");

<强> deleteThis.php

$success = false;
$msg = '';
if(isset($_POST['oid'])) { 
    //delete $_POST['oid'] here        
    $success = true; //if successfull
    $msg = 'Deleted ok';
} else {
    $msg = 'No ID sent';
}

//send json data back
echo json_encode( array('msg'=>$msg, 'success' => $success) );