gpt4 book ai didi

javascript - 如果在 codeigniter 中使用 ajax 成功更新,则发出警报

转载 作者:行者123 更新时间:2023-11-29 19:46:03 24 4
gpt4 key购买 nike

请帮帮我。我是 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>

我的主 Controller 显示

$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.
}
}

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

这是我的 Controller :

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。但我似乎无法理解它..我只是编程新手。平均水平。

最佳答案

试试这个:
更新
型号:

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

Controller :

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。

关于javascript - 如果在 codeigniter 中使用 ajax 成功更新,则发出警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19626839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com