如何在不刷新页面的情况下在另一个函数中调用删除函数?

时间:2019-01-21 11:57:29

标签: php ajax codeigniter

我目前正在开发一个程序,用户可以根据需求选择零件数量并以表格形式显示,还允许他们删除自己选择的项目而无需刷新页面。 我的查看页面是:

 <div class="col-md-6">    <h4>Estimate</h4>
                  <div  id="estimate">

              </div>

                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal"> + Add Part/Service</button>
                  </div>

                </div>  
                <div class="form-actions">

                  <div class="pull-right">
                    <button class="btn btn-danger btn-cons" type="submit" name="submit" value="Submit"><i class="icon-ok"></i> Save</button>
                    <button class="btn btn-white btn-cons" type="button">Cancel</button>
                  </div>
                </div>

              </form>
            </div>
          </div>
        </div>
      </div>

我正在显示有关div id估计的结果。

我的控制器功能代码是:

      function estimate(){
$this->load->library('session');
$this->load->module('site_security');
$this->site_security->_make_sure_is_user();

if (!empty($this->input->post())) {
    $output = " ";

        $estimate_code = $this->input->post('estimate_code');
        $part_name = $this->input->post('part_name');
        $part_code = $this->input->post('part_code');
        $customer_price_with_gst = $this->input->post('customer_price_with_gst');
        $part_group_name = $this->input->post('part_group_name');
        $bname = $this->input->post('bname');


        $sql = "INSERT INTO `estimate`(`id`, `estimate_code`, `part_name`,`part_code`,`amt`,`part_group_name`,`brand`) VALUES
        ('','$estimate_code','$part_name','$part_code','$customer_price_with_gst','$part_group_name','$bname')";
        if ($this->_custom_query($sql)) {

            $query = "SELECT * FROM estimate where estimate_code ='$estimate_code'";
            $result = $this->_custom_query($query);

            $output .='  <table class="table table-hover no-more-tables">
                        <thead>
                          <tr>
                            <th>S.No</th>
                            <th>Part Code</th>
                            <th>Part Description</th>
                            <th>Amount</th>

                          </tr>';
                         $s = 1; $amt1 = "";
                          foreach ($result->result() as $row) {
                            $brand= $row->brand;

                            $s1[] =  $row->part_group_name;
                            $id = $row->id;
                            $amt = $row->amt;
                             $amt1 = $amt1+$amt;
                            $output .= '<tr>
                            <th>'.$s++.'</th>
                            <th>'.$row->part_code.'</th>
                            <th>'.$row->part_name.'</th>
                            <th>'. $amt.'</th>
                            <th>
                                 <form class="estimatedel" name="estimatedel">
                                  <input name="id" value="'.$id.'" type="hidden">
                                    <button type="button" id="estimatedel" value="estimatedel" class="btn btn-primary" data-dismiss="modal"onclick="return ajaxFunction();"><i class="fa fa-times"></i></button></form>

                                    </th>
                          </tr>';

                         } $counter = 0;
                     $sam = $s1;
                  $Query = "select max(total) as h from service_charge ";
                 if(count($sam) > 0)
               $Query .= " where ( ";
               foreach($sam as $Filtervalues)
                 {
                $counter++;
              $Query .= "part_group_name = '$Filtervalues'";
             if($counter != count($sam))
             $Query .= " or ";
              else
              $Query .= ")";
                }
             $result = $this->_custom_query($Query);
              foreach ($result->result() as $row1) {
$tot1=0;
$tot=$row1->h;

     }
          $total_amt = $tot+$amt1;

                          $output .=' <input type="hidden" name="estimate_code" value="'.$estimate_code.'">
                          <input type="hidden" name="estimate_amt" value="'.$amt1.'">
                          ';

        }
            echo $output;

            print_r('<input type="hidden" name="service_amt" value="'.$tot.'">
                <input type="hidden" name="total_amt" value="'.$total_amt.'">



                <tr><td colspan="3">Amount: </td><td style= "text-align:left;" colspan="2">'.$amt1.'</td></tr><tr><td colspan="3">Service Charge: </td><td style= "text-align:left;" colspan="2">'.$tot.' </td></tr><tr><td colspan="3">Total Amount: </td><td style= "text-align:left;" colspan="2">'.$total_amt.' </td></tr>  </thead> </table>');
        }

     }

我被困在删除部分上,用户可以在其中删除任何选定的部分而无需刷新或丢失选定的部分。

1 个答案:

答案 0 :(得分:0)

创建在复选框更改事件时触发的jQuery函数,并检查是否选中了复选框。如果未选中该复选框,则仅拨打电话将其从列表中删除。可能在下面的代码会帮助您。

if($(this). prop("checked") == false){
  var checkboxid = $(this).attr('id');
  $.ajax({
      method:post,
      url: "remove_part.php", 
      data:{
       chkid:checkboxid
      }
      success: function(result){
          alert('removed successfully');
      }
  });
}