Search code examples
phpcodeigniterpostinputsubmit

Codeigniter $this->input->post() is empty for submit input


<form method="post" id="user_form">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Add User</h4>
            </div>
            <div class="modal-body">
                <label>Enter First Name</label>
                <input type="text" name="first_name" id="first_name" class="form-control" />
                <br />
                <label>Enter Last Name</label>
                <input type="text" name="last_name" id="last_name" class="form-control" />
                <br />
                <label>Select User Image</label>
                <input type="file" name="user_image" id="user_image" />
                <span id="user_uploaded_image"></span>
            </div>
            <div class="modal-footer">
                <input type="hidden" name="user_id" id="user_id" />
                <input type="submit" name="action" id="action" class="btn btn-success" value="Add" />
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </form>

and script in this view:

$.ajax({
                    url:"<?php echo base_url() . 'raportowanie/user_action'?>",
                    method:'POST',
                    data:new FormData(this),
                    contentType:false,
                    processData:false,
                    success:function(data)
                    {
                        alert(data);
                        $('#user_form')[0].reset();
                        $('#userModal').modal('hide');
                        dataTable.ajax.reload();
                    }
                });

in controller

    function user_action(){

    echo 'Action is: '.$this->input->post('action');
}

The problem is that $this->input->post('action') don't return value input. If I change the type input on text this $this->input->post('action') is ok and return value = Add. Why this don't work on input type="submit" ? How can i get the value of my input. Please help.


Solution

  • The submit input type is just for the submit button which submits all form values and not usually used to send data itself. The value attribute is used as the button's label.

    If you want to see if there has been a post request to your controller you could use something like this:

    function user_action()
    {
        if ($this->input->server('REQUEST_METHOD') == 'POST')
        {
            // Your other inputs here
            echo 'First Name is: '.$this->input->post('first_name');
            echo 'Last Name is: '.$this->input->post('last_name');
            echo 'User Image is: '.$this->input->post('user_image');
            echo 'User ID is: '.$this->input->post('user_id');
        }
    }
    

    Or if using Codeigniter v4 you could also use $this->request->isAJAX(). I'm not sure if this was available in v3 or not.

    UPDATE

    If you want to check if its add/edit I would suggest adding a hidden input and change that value as well like below:

    <input type="hidden" name="method" value="add">
    

    Then you can check it in the controller:

    if ($this->input->post('method') == 'add')
    {
        // Adding a record
    } else {
        // Editing a record
    }