how to return success message in session variaible using jquery.get() method
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']
)
- how do i set success message on a session variable and show on specific span ?
or
- is there any other method in jQuery that a mes开发者_如何转开发sage 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
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) );
精彩评论