2

请帮帮我。我是ajax新手。我正在使用 codeigniter.. 我正在使用 javascript 模式(警报)来显示我是否成功删除了数据。

这是我的看法:

<script type="text/javascript">
    $(document).on("click", ".delete-data", function () {
        var id = $(this).data('id');
        $("#id").val( id );
    });
</script>
<div class="down1">
    <button type="button" class="button-orange" onclick="add_child();" id="child_add">Add</button>
</div>
<div class="span11 offset1" id="childdv">
     <?php echo $child_set;?> <!--this one shows that table of children-->
</div>
the modal delete
<!--delete-->
            <div class="modal fade" id="delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-haspopup="true" aria-hidden="true">
                <div class="modal-dialog">
                <div class="modal-content">
                    <div class="alert alert-danger fade in" id="alert">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h3>Heads Up!</h3>
                        <p>What you are doing will delete a data!</p>
                        <input type="hidden" name="id" id="id" value=""/>
                        <a onclick="delete_child();" id="okButton" class="btn btn-danger" data-dismiss="modal">
                              Confirm Delete
                        </a>
                        <a href="#" class="btn" data-dismiss="modal">
                              Cancel
                        </a>
                    </div>
                </div>
                </div>
            </div>

我的主控制器显示

$data_set['record_set'] = $this->emp->get_children($id);
$data['employee_header_menus'] = $this->load->view('employee_header_menus', NULL, TRUE);
$data['employee_header_logout'] = $this->load->view('employee_header_logout', NULL, TRUE);
$data['child_set'] = $this->load->view('swoosh_template/employee/child', $data_set, TRUE);  //<--- you can see in here that the $child_set came from here.
$data['info'] = $this->emp->get_myinfo($id);
$data['msg'] = "";
$data['color'] = "green";
$data['header_logo'] = $this->load->view('header_logo', NULL, TRUE);
$this->load->view('employee/personaldetails', $data);

现在。那个 child_set 在哪里。这是一个视图。

<?php if ($record_set !="" ) { ?> 
<table id="tblcount" class="table table-bordered">
<tr class="orange">
    <th>Name</th>
    <th>Date of Birth</th>
    <th>Dependent</th>
    <th>Delete</th>
</tr>
 <?php foreach ($record_set as $row): ?>
<tr>
    <td>
        <?php echo $row[ 'name'];?>
    </td>
    <td>
        <?php echo $row[ 'birth_date'];?>
    </td>
    <td>
        <?php echo ($row[ 'dependent']=='1' ? 'Yes': 'No');?>
    </td>
    <td>
        <img data-toggle="modal" data-id="<?php echo $row['id']?>" class="delete-data" href="#delete" style="cursor:pointer" height="15" width="15" src="<?php echo base_url(); ?>images/remove.gif">
    </td>
</tr>
<?php endforeach?>
</table>

这是我调用模态的javascript。

function delete_child()
{   
  var id = $("#id").val(); 
  if (id!=null){
      swoosh(id, path+'swoosh_employee/swoosh_delete_child', 'childdv'); //<-- this is the function that calls my controller
      $('#success').modal('show');  // <-- this is the alert.
  }  
}

但我不想把警报放在那里。因为我想首先验证我的控制器中的功能是否成功运行。这就是我将显示警报的时间。

这是我的控制器:

public function swoosh_delete_child()
{
    $P1 = $this->session->userdata('id');
    parse_str($_SERVER['QUERY_STRING'],$_GET);
    $id = $_GET['h'];

    $this->emp->delete_children($id); //  <-- in here , i want to validate this. if successful: alert success, else alert error

    $set['record_set'] = $this->emp->get_children($P1);
    $this->load->view('swoosh_template/employee/child',$set);
}

这是模型:

public function delete_children($P1)
{
    $this->db->delete('employee_children', array('ID' => $P1)); 
}

现在 。我想做的是。“ajax”..这将检查函数是否成功运行或者我是否成功删除,例如:

if success{
   $('#success').modal('show');
}
else{
   $('#error').modal('show');
}

我一直在寻找如何使用ajax。但我似乎无法理解它.. 我只是编程新手。平均。

4

1 回答 1

3

试试这个:
更新
模型:

public function delete_children($P1,$id)
{
    $delete = $this->db->delete('employee_children', array('ID' => $P1,'child_id' => $id)); 
    if ($delete){
     return "success";
    }
    else{
     return "failed";
    }
}

控制器 :

public function swoosh_delete_child()
    {
        $P1 = $P1 = $this->session->userdata('id'); // get the parent id
        $id = $this->input->post('id'); // get child id

        $delete = $this->emp->delete_children($P1,$id); // delete the child of current parent
        if($delete == "success"){
        echo $delete;
        }
        else{
        echo $delete;
        }
    }

我曾经使用过这个 ajax:

function delete_child()
{   
  var id = $("#id").val();
  if (id!=null){

   $.ajax({
   url : "<?php echo base_url('swoosh_employee/swoosh_delete_child') ?>",
   data : "id="+id,
   type: "POST",
   success : 
   function (data){
     if (data == "success"){
     alert("delete success");
     }
     else{
     alert("delete failed");
     }
   } 
   }); 
  }  
}

要显示此父级的子级,您可以构建另一个 ajax。

于 2013-10-28T04:03:39.117 回答