CodeIgniter中的一个表单上的两个按钮

时间:2015-07-07 17:04:31

标签: php ajax codeigniter web

我的目标是将两个按钮分配给一个表单。单击其中一个按钮,编辑故障单,第二个按钮删除故障单。表单通过AJAX提交给控制器,控制器然后适当地指示操作。到目前为止我所尝试的是通过AJAX将提交值发送到控制器,然后在那里读取,并调用适当的模型来更新数据库,但它无法正常工作。

以下是相关代码:

表单提交按钮(在视图中):

// Prints the memory location of the pointers
printf ("%d and %d\n", &p, &q);

// Prints the values of the pointers
printf ("%d and %d\n",  p,  q);

// Prints the values that the pointers point to
printf ("%d and %d\n", *p, *q);

接收控制器:

<div class="form-group">
    <div class="col-sm-5 col-sm-offset-7">
        <input id="type" style='font-size:18px; margin-left: 35px; margin-top: 10px;' name="submit" type="submit" value="Edit Ticket" class="btn btn-lg btn-success">
    </div>
    <div class="col-sm-3 col-sm-offset-9">
        <input id="type" style='font-size:18px; margin-left: 35px; margin-top: -65px;' name="submit" type="submit" value="Delete Ticket" class="btn btn-lg btn-danger">
        </div>
</div>

模特:

public function editExistingTicket(){
    $this->load->model('tickets');
    $ticketId = $this->input->post('ticketId');

    if ($this->input->post('submit') == 'Edit Ticket') {
        $ticket = array(
                  'ticketId'    => $ticketId,
                  'category'    => $this->input->post('category'),
                  'headline'    => $this->input->post('headline'),
                  'description' => $this->input->post('description'),
                  'assigned'    => $this->input->post('assigned'),
                  'open'        => $this->input->post('status'),
                  'priority'    => $this->input->post('priority')
                );
        $edited = $this->tickets->editTicket($ticket);
    } 
    if($this->input->post('submit') == 'Delete Ticket') {
        $this->tickets->deleteTicketById($ticketId);
    }
}

我错过了一些简单的事吗?这样做的策略是不正确的?

感谢您的帮助!!

2 个答案:

答案 0 :(得分:1)

两个提交按钮的ID应该不同,如

$("#type1").click(function(){
  // Your code
  return false;
});

$("#type2").click(function(){
  // Your code
  return false;
});
<div class="form-group">
    <div class="col-sm-5 col-sm-offset-7">
        <input id="type1" style='font-size:18px; margin-left: 35px; margin-top: 10px;' name="submit" type="submit" value="Edit Ticket" class="btn btn-lg btn-success">
    </div>
    <div class="col-sm-3 col-sm-offset-9">
        <input id="type2" style='font-size:18px; margin-left: 35px; margin-top: -65px;' name="submit" type="submit" value="Delete Ticket" class="btn btn-lg btn-danger">
        </div>
</div>

答案 1 :(得分:0)

只需改变他们的名字,而不是他们的价值:

<div class="form-group">
    <div class="col-sm-5 col-sm-offset-7">
        <input name="edit" type="submit" value="1" id="type" style='font-size:18px; margin-left: 35px; margin-top: 10px;'  class="btn btn-lg btn-success">
    </div>
    <div class="col-sm-3 col-sm-offset-9">
        <input name="delete" type="submit" value="1" id="type" style='font-size:18px; margin-left: 35px; margin-top: -65px;' class="btn btn-lg btn-danger">
    </div>
</div>

控制器:

public function editExistingTicket()
{
    $this->load->model('tickets');
    $ticketId = $this->input->post('ticketId');

    if ($this->input->post('edit')) {
        $ticket = array(
            'ticketId'    => $ticketId,
            'category'    => $this->input->post('category'),
            'headline'    => $this->input->post('headline'),
            'description' => $this->input->post('description'),
            'assigned'    => $this->input->post('assigned'),
            'open'        => $this->input->post('status'),
            'priority'    => $this->input->post('priority')
        );
        $edited = $this->tickets->editTicket($ticket);
    } elseif ($this->input->post('delete')) {
        $this->tickets->deleteTicketById($ticketId);
    }
}