views:

235

answers:

1

Hello All,

i m using jQuery.get() to delete row from a table from a page main.php. now i want to show success message after deleting row ,that success message should be in session variable($_session['suxesMsg'])

  1. how do i set success message on a session variable and show on specific span ?

or

  1. is there any other method in jQuery that a message appear for 5-10 seconds only and then disappear?

Here is my code

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.";
    }

Thanks for helping me alwayz

+2  A: 

I'm not sure why it needs to go into the session. Looking at your code this would mean you need to refresh the page before the actual error message is shown, because it is done in PHP when the page is originally generated.

Could you not return a success message directly in the request which deleted the item, and then show it immediately using javascript? You could use JSON to encode a success flag and a string message which is sent back. For example something like this:

//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) );
Tom Haigh
To get the result of the get(), pass a success-handler. Here you can update your span.
ZeissS
@Tom yes u r right ,i need to refresh the page before the actual error message is show, to avoid that i want solution sir
diEcho
@I Like PHP: updated my answer
Tom Haigh
Thank for response . but I don't want alert message, i just want to show a message on page, that's why i use session, bcoz that message comes with a style embedded on span tag, please see the main.php code
diEcho
@I Like PHP: That was just an example to demonstrate how it might work. I updated again. You'll need to change the PHP so `<span class="msg"></span>` is always on the page.
Tom Haigh
Thankyou very very much Sir
diEcho
I did this i use this one`$('#suxes').addClass('msg').html('Success:'+data.msg).fadeOut(3000);`
diEcho