为什么我的表单数据不会在CodeIgniter中从一个页面处理到另一个页面?

I am developing the admin panel for my web application. On the index of the member management section is a list of the newest member accounts. There is a checkbox for each member that an admin can check to delete multiple users at once. I use jQuery to submit the form with a link. Since the form is on the index of the member management section(members/home), I want the process of the form to be on members/delete. When the members/delete controller is accessed, there is an option of providing a single member ID in a URL segment or the admin can use the form on the members index page.

My problem is that none of the data from the checkboxes is being passed to the members/delete controller. I have tried $this->input->post('delete_members') and $_POST['delete_members']. I have also tried changing the method of the form to GET and then $_GET['delete_members']. I get "Undefined index: delete_members with everything I try. Any ideas?

PS: I have a route that changes members/delete to members/home/delete

Members controller: (members/home)

<?php

class Home extends MY_Controller {

    function __construct(){

        parent::__construct();

    }

    function index($page = 1){

        $this->template->overall_header("Manage Members", array(), array(libs_url('jquery-datatables/extensions/TableTools/css/dataTables.tableTools.css'), libs_url('jquery-datatables/css/dataTables.bootstrap.css')));

        $this->load->library('pagination');

        $data['member_results'] = $this->member->result_array($this->admin['results_per_page'], $page);

        $config = array(
            'base_url' => site_url('members/'),
            'total_rows' => $this->member->total_rows,
            'per_page' => $this->admin['results_per_page'],
            'use_page_numbers' => TRUE,
            'full_tag_open' => '',
            'full_tag_close' => '',
            'first_link' => '<i class="fa fa-angle-double-left"></i>',
            'first_tag_open' => '<li>',
            'first_tag_close' => '</li>'.PHP_EOL,
            'last_link' => '<i class="fa fa-angle-double-right"></i>',
            'last_tag_open' => '<li>',
            'last_tag_close' => '</li>
',
            'next_link' => '<i class="fa fa-angle-right"></i>',
            'next_tag_open' => '<li>',
            'next_tag_close' => '</li>'.PHP_EOL,
            'prev_link' => '<i class="fa fa-angle-left"></i>',
            'prev_tag_open' => '<li>',
            'prev_tag_close' => '</li>'.PHP_EOL,
            'cur_tag_open' => '<li class="active"><a href="">',
            'cur_tag_close' => '</a></li>'.PHP_EOL,
            'num_tag_open' => '<li>',
            'num_tag_close' => '</li>'.PHP_EOL,
            'cur_page' => $page,
            'uri_segment' => 2
        );

        $this->pagination->initialize($config);

        $data['pagination'] = $this->pagination->create_links();

        $this->load->view('members/index_body', $data);

    }

    function delete($member_id = ''){

        /* If a member id wasn't provided in the URL, look for the array of checkboxes */
        if($member_id == ''){

            if(count($_POST['delete_members']) > 0){

                foreach($_POST['delete_members'] as $id){

                    $this->member->delete($id);

                }

                if($this->member->error == NULL){

                    $this->template->overall_header("Request Processed");

                    $data = array(
                        'error_title' => 'Request Processed',
                        'error_message' => 'The members you have requested have been deleted from the database.',
                        'redirect' => 'members'
                    );

                    $this->load->view('error_body', $data);

                } else {

                    $this->template->overall_header("System Error");

                    $data = array(
                        'error_title' => "System Error",
                        'error_message' => $this->member->error,
                        'redirect' => 'members'
                    );

                    $this->load->view('error_body', $data);

                }

            } else {

                $this->error->title('No Members Selected')->message('You did not select any members to delete!')->redirect('members')->display();

            }

        } else {

            $this->member->delete($member_id);

            if($this->member->error == NULL){

                $this->template->overall_header("Request Processed");

                $data = array(
                    'error_title' => "Request Processed",
                    'error_message' => 'The member you requested has been deleted from the database.',
                    'redirect' => 'members'
                );

                $this->load->view('error_body', $data);

            } else {

                $this->template->overall_header("System Error");

                $data = array(
                    'error_title' => "System Error",
                    'error_message' => $this->member->error,
                    'redirect' => 'members'
                );

                $this->load->view('error_body', $data);

            }

        }

    }

The form:

if(count($member_results) > 0){

    $i = 0;

    $str .= '
                            <div class="table-responsive">
                                '.form_open('members/delete', 'id="delete" role="form"').'
                                    <table class="table table-striped" data-sortable">
                                        <thead>
                                            <tr>
                                                <th></th>
                                                <th>Display Name</th>
                                                <th>Email</th>
                                                <th>Join Date</th>
                                                <th>Actions</th>
                                            </tr>
                                        </thead>
                                        <tbody>
            ';

    foreach($member_results as $member){

        $str .= '
                                            <tr>
                                                <td><input type="checkbox" name="delete_members[]" value="'.$member['id'].'"></td>
                                                <td>'.anchor('members/modify/'.$member['id'], $member['display_name']).'</td>
                                                <td>'.$member['email_address'].'</td>
                                                <td>'.$member['date_joined'].'</td>
                                                <td>
                                                    <div class="btn-group btn-group-xs">
                                                        '.anchor('members/modify/'.$member['id'], '<i class="fa fa-pencil-square"></i>', 'data-toggle="tooltip" title="Modify '.$member['display_name'].'" class="btn btn-danger"').'
                                                        '.anchor('members/delete/'.$member['id'], '<i class="fa fa-eraser"></i>', ' data-toggle="tooltip" title="Delete '.$member['display_name'].'" class="btn btn-danger"').' 
                                                    </div>                          
                                                </td>
                                            </tr>
            ';

    }

    $str .= '
                                        </tbody>
                                    </table>
                                '.form_close().'
                            </div>
                            <div class="data-table-toolbar">
                                    <ul class="pagination">
                                      '.$pagination.'
                                    </ul>
                                </div>
        ';
}

$str .= '<script>
           $(\'#submit-delete\').on(\'click\', function(e){
if($(\'input[type="checkbox"][name="delete_members[]"]:checked\').length > 0){
    $(\'#delete\').submit();
} else {
    alert(\'You did not select any members to delete!\');
    e.preventDefault();
}

}); ';

echo $str;
doupuzhimuhan9216
doupuzhimuhan9216 它位于JS标签内部的底部$('#delete')。submit()
接近 6 年之前 回复
doudao1369
doudao1369 哪里'是jQuery提交代码和重定向?无法使用javascript重定向到期望发布数据的网页
接近 6 年之前 回复

3个回答



试试这个:</ p>

替换此行:</ p>

  form_open('members / delete','id =“delete”role =“form”')
</ code> </ pre>

使用:</ p>

form_open('home / delete','id =“delete”role =“form”')
</ code> </ pre>
</ div>

展开原文

原文

Try this:

Replace this line:

form_open('members/delete', 'id="delete" role="form"')

With:

form_open('home/delete', 'id="delete" role="form"')

douyun6781
douyun6781 对不起,我应该提到我正在使用路由来更改成员/删除成员/ home / delete。 这就是为什么我得到PHP错误而不是404找不到:)
接近 6 年之前 回复



更改方法</ p>

  function delete($ member_id =''){
//你的代码在这里
}
</ code> </ pre>

到</ p>

  function delete(){
echo'&lt ;预&GT;'; 的print_r($ _ POST); 死了;
//您的代码在这里
}
</ code> </ pre>

检查您的表单帖子是否有少量方法。 它在POST方法中输入删除方法你可以根据逻辑做其余的代码。</ p>
</ div>

展开原文

原文

Change method

function delete($member_id = '') { 
  // your code here
}

to

function delete() { 
 echo '<pre>'; print_r($_POST); die;
 // your code here
}

Check of your form post is working in few method or not. It POST data in coming in delete method you can do rest of code according to logic.



我意识到由于我购买的主题附带的jQuery iCheck插件,没有发布任何复选框值。 当我对主题持怀疑态度时,我禁用了JS,你不知道,复选框值开始发布了。</ p>
</ div>

展开原文

原文

I realized that none of the checkbox values were being posted because of the jQuery iCheck plugin that came with a theme that I purchased. I disabled JS when I became skeptical of the theme and, wouldn't you know it, the checkbox values started to post.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐