0
<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.

0

3 Answers 3

1

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. But I want in controller check the input value because in another js script I changing this value. I check if the value is equal Add or Edit. Like that $('#action').val("Edit"); I use Codeigniter v3
1

Use a hidden field to pass value, not a submit button

You can create a field value, and change the value based on last button you click

<form>
    <input type = "hidden" id = "btn_type" name = "btn_type">
    <button type="submit" class="btn btn-primary" id = "btn_add">Add</button>
    <button type="submit" class="btn btn-primary" id = "btn_edit">Edit</button>
</form>

Change your hidden value when the button is clicked

$(document).on("click", "#btn_add", function(event){
    $('#btn_type').val('Add');
});

$(document).on("click", "#btn_edit", function(event){
    $('#btn_type').val('Edit');
});

And get the value in your controller

$buttonvalue = $this->input->post('btn_type',true);

Comments

0

Thank you very much for help. Now I have in view:

 <input type="hidden" name="user_id" id="user_id" />
 <input type="hidden" name="method" id="method" value="Add">
 <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>

and I set the value add or edit in JS script

        $('#action').val("Add"); \\ or Edit
        $('#method').val("Add"); \\ or Edit

In controller

$this->input->post('method')

This works perfectly. Thanks !!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.