简体   繁体   English

在codeigniter中使用jquery显示确认消息

[英]showing confirmation message using jquery in codeigniter

Good day all, I'm new to CodeIgniter and I really need some help. 一切顺利,我是CodeIgniter的新手,我真的需要一些帮助。 I'm trying to make a confirmation message(div id="delmsg") using jquery when deleting a mysql record. 我正在尝试在删除mysql记录时使用jquery发出一条确认消息(div id =“ delmsg”)。 But only the delete operation works but I don't see any message. 但是只有删除操作有效,但是我没有看到任何消息。

Code for Model page: Model页面代码:

function delete_post($postID)
{
    $this->db->where('post_id', $postID);
    $this->db->delete('khanposts');
}

Code for Controller page: Controller页面代码:

function deletepost($postID)
{
    $this->khanpost->delete_post($postID);
    redirect(base_url().'khanposts/index/');
}

Code for View page: View页面代码:

<div id="main_body">
if (!isset($posts)){
echo "<p>There are currently no active posts.</p>";
}else{
echo '<table align="center">';
echo '<tr><th>Email Address</th><th>Contact No.</th><th>Actions</th></tr>';
foreach ($posts as $row){
    echo '<tr><td><i>' .$row['email']. '</i></td><td><b>' .$row['contact']. '</b></td>
    <td><a href="' .base_url(). 'khanposts/deletepost/' .$row['post_id']. '" id="delete"><i>Delete</i></a></td></tr>';
}
echo '</table><br><br>';
echo '<br><br><div id="delmsg"></div>';
}
</div>

<script>
$('#delete').click(function() {
$("#delmsg").css("visibility", "visible");
$('<h3>Record Deleted!</h3>').appendTo('#main_body');
});
//return false;
});
</script>

Is there any way that I could use jquery message in codeigniter? 有什么办法可以在codeigniter中使用jquery消息? And also do I've to load any jquery plugin file manually in codeigniter? 而且我是否必须在codeigniter中手动加载任何jquery插件文件? Any help would be grateful. 任何帮助将不胜感激。 Tnx. Tnx。

You can do this by an Ajax call, you shouldn't show deleted message until you get success return from your request, and you need to use jQuery library to do that, Codeigniter itself doesn't provide you this, so you have to import jQuery in your view file like this: 您可以通过Ajax调用来做到这一点,在请求成功返回之前,您不应该显示已删除的消息,并且您需要使用jQuery库来做到这一点,Codeigniter本身不提供此功能,因此您必须导入jQuery在您的视图文件中是这样的:

<script src="//code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

To make an Ajax call, add a data attribute which will hold the id of the that item and remove the href attribute as this will make your page to go to that page if you don't prevent that, also id tags must be unique so change it to class: 要进行Ajax调用,请添加一个数据属性,该属性将保存该项目的ID,并删除href属性,因为如果您不这样做,这将使您的页面转到该页面,而且ID标记也必须是唯一的,因此将其更改为类:

foreach ($posts as $row){
    echo '<tr><td><i>' .$row['email']. '</i></td><td><b>' .$row['contact']. '</b></td>
    <td><a data-id="' .$row['post_id']. '" href="#" class="delete"><i>Delete</i></a></td></tr>';
}

Then, make the Ajax call like this: 然后,像这样进行Ajax调用:

$(".delete").on('click',function(){
     $.ajax({
         url: "<?php echo site_url('khanposts/deletepost');?>",
         type: 'POST',
         data: { "post_id": $(this).data("id") },
         success: function() {
             $("#delmsg").css("visibility", "visible");
             $('<h3>Record Deleted!</h3>').appendTo('#main_body');
         }
     });
 });

Then in your controller, get the post value like this, both the ajax method or browsing that page method can work if we configure it like this : 然后在您的控制器中,获取这样的post值,如果我们这样配置ajax方法或浏览该page方法,则它们都可以工作:

function deletepost($postID = null, $redirect = true)
{
    if($postID == null){
        $postID =  $this->input->post('post_id');
        $redirect = false;
    }
    $this->khanpost->delete_post($postID);
    if($redirect)
        redirect(base_url().'khanposts/index/');
}

Your'e redirecting the page, clicking on a link, in this situation you should show the delete confirmation message when you land back to the posts page: 您正在重定向页面,单击链接,在这种情况下,当您返回到帖子页面时,应该显示删除确认消息:

function deletepost($postID)
{
    $this->khanpost->delete_post($postID);
    $this->session->set_flashdata('delmsg', 'Record Deleted!'); //this sets a one time accessable flash data
    redirect(base_url().'khanposts/index/');
}

Now, to show this message in your view: 现在,要在您的视图中显示此消息:

echo '<br><br><div id="delmsg"><h3>'.$this->session->flashdata('delmsg').'</h3></div>';

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

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