在CodeIgniter中提交在AJAX成功函数中动态创建的表单

时间:2015-07-17 18:11:50

标签: javascript php html ajax codeigniter

我有一个我在AJAX调用的成功函数中动态创建的表单。我认为,一旦这个表单附加到DOM,它将像CodeIgniter中的普通表单一样提交。但是,当我单击提交按钮时,它会被发送到相应的控制器,但没有一个POST数据被接收。

我的初始形式:

 <div id="editTicketOptions" class="alert alert-info" style="display:none">
     <form name="editTicketForm" id="editTicketForm" action="">
      <div class="form-group">
          <label for="edit_ticket">Choose a Ticket:</label>
          <select name="edit_number" id="edit_number" class="form-control">
               <option value="">Choose one...</option>
               <?php foreach ($created_tickets as $ticket): ?>
                    <option value="<?php echo($ticket->ticketId); ?>"><?php echo ($ticket->ticketId . " - " . $ticket->headline); ?></option>
               <?php endforeach; ?>
          </select>
       </div>
    </form>
</div>

我的AJAX电话:

$('#edit_number').change(function(){
    $.ajax({
        type:"POST",
        url: "<?php echo base_url('ticket_system/edit_details'); ?>",
        dataType: "json",
       data:{
            'ticketId': $(this).val()
       },
       success: function(ticket){
            $("#headline_entry").remove();
            $("#description_entry").remove();
            $("#priority_entry").remove();
            $("#button_entry").remove();
            $("#editTicketForm").append("<div class='form-group' id='headline_entry'><label for='edit_headline'>Headline:</label><input type='text' class='form-control' name='edit_headline' id='edit_headline' required></div>");
            $("#editTicketForm").append("<div class='form-group' id='description_entry'><label for='edit_description'>Description:</label><textarea class='form-control' rows='3' name='edit_description' id='edit_description' required>" + ticket[0].description + "</textarea></div>");
            $("#editTicketForm").append("<div class='form-group' id='priority_entry'><label for='edit_priority'>Choose priority:</label><select name='edit_priority' id='edit_priority' class='form-control'><option selected value=" + ticket[0].priority + ">" + ticket[0].priority + "</option><option value='High'>High</option><option value='Medium'>Medium</option><option value='Low'>Low</option></select>");
            $("#editTicketForm").append("<div class='text-center' id='button_entry'><input class='btn btn-primary' type='submit' id='editTicketButton' value='Edit Ticket'></div>");
            $("#editTicketForm").attr("action", "<?php echo base_url('ticket_system/edit_ticket/'); ?>" + "/" +ticket[0].ticketId + " ");
        }
    });
});

我的控制器功能:

public function edit_ticket()
{
    $ticketId = $this->uri->segment(3);
    $headline = $this->input->post('edit_headline');
    $description = $this->input->post('edit_description');
    $priority = $this->input->post('edit_priority');
    $edit_ticket = array(
        'ticketId' =>$ticketId,
        'headline' => $headline,
        'description' => $description,
        'priority' => $priority
    );

    $this->tickets->editTicket($edit_ticket);

    redirect('ticket_system/view_tickets');
}

我知道它不是最漂亮的代码,但我在这里错过了一些简单的东西吗?有更好的方法吗?

感谢您的帮助!!

1 个答案:

答案 0 :(得分:1)

问题是您的控制器方法edit_ticket()正在接收POST信息$this->input->post(...),但您没有将表单设置为方法POST,因此发送GET信息而不是POST。

解决方案:将属性method="POST"添加到表单中:

<form name="editTicketForm" id="editTicketForm" method="POST">