0

So I am loading data into a form and the user should be able to change it and save it. The data loads fine and when the user clicks on the save button, jQuery grabs that data and should send it to my codeignighter controller, but it does not. The weird thing is that codeignighter is saying that the call was successful.

Here is my jQuery (it is in a seperate .js file):

$(document).ready(function(){
    $("#checkin").submit(function(e){
        e.preventDefault();
        var ConditionID = [];
        $('input[name^="ConditionID"]').each(function() {
            ConditionID.push($(this).val());
        });
        var Field = [];
        $('div[name^="Field"]').each(function() {
            Field.push($(this).val());
        });
        var Status = [];
        $('select[name^="Status"]').each(function() { 
            Status.push($(this).val());
        });
        var Description = [];
        $('textarea[name^="Description"]').each(function() {
            Description.push($(this).val());
        });
        $.ajax({
            url: "/index.php/ConditionReports/saveCheckin",
            type: "POST",
            data: {'ConditionID':ConditionID,'Field':Field,'Status':Status,'Description':Description},
            success:function(data)
            {
                alert('SUCCESS!!');
            },
            error:function(jqXHR, textStatus) {
            alert( "Request failed: " + textStatus );
            }
        });
    });
});

My view:

<form action='' method='post' id='checkin' accept-charset='ytf-8'>
    <?php
    $i=0;
    foreach($Conditions as $c) {
        echo("
        <input type='hidden' id='ConditionID' name='ConditionID[]' value='$c->ConditionID' />
        <div id='Field' name='Field[]' value='$c->Field'>$c->Field</div>
        <select id='Status' name='Status[]'>");
            $e=($c->Status=="E"?"selected":"");
            $g=($c->Status=="G"?"selected":"");
            $s=($c->Status=="S"?"selected":"");
            $b=($c->Status=="B"?"selected":"");
            $m=($c->Status=="M"?"selected":"");
            echo("
            <option $e value='E'>Excellent/New</option>
            <option $g value='G'>Good</option>
            <option $s value='S'>Satisfactory</option>
            <option $b value='B'>Poor/Needs Repair</option>
            <option $m value='M'>Missing</option>
        </select>
        <textarea class='form-control' name='Description[]' id='Description'>$c->Description</textarea>
        ");
        $i++;
    }
    ?>
<button type="button" class="btn btn-info">Print</button>
<input type="submit" id="saveCheckin" value='Save' class="btn btn-success save">
<button type="button" class="btn btn-success">Check-In</button>
</form>

My controller:

class ConditionReports extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->model('ConditionReportsVM');
        $header_data['js'] = array('jquery-2.2.1.min','checkin');
        $this->load->view('header', $header_data);
    }
    public function saveCheckin()
    {
        $data = array(
            'ConditionID' => $this->input->post('ConditionID'),
            'Field' => $this->input->post('Field'),
            'Status' => $this->input->post('Status'),
            'Description' => $this->input->post('Description'),
        );
        $this->ConditionReportsVM->UpdateReport($data);
    }
}

And lastly my model:

public function UpdateReport($data)
{
    $this->db->where('ConditionID',$data['ConditionID']);
    $this->db->update('Conditions',$data);
}

I have been stuck on this for quite a bit of time now and any help I can get is most appreciated. I have debugged jQuery and found out that the appropriate data is being called. There are no console errors. I have tried to echo something from the controller and I have also tried to redirect the page from the controller but the controller just seems like it is not being hit.

Thank you in advance.

Note: Currently the response I get after I click save is an alert with this in it: "SUCCESS!!"

8
  • Does it go saveCheckin action on Button click ? Commented Apr 8, 2016 at 13:22
  • Console.log your post variables before you send them with the ajax, they might be empty. If not, then var_dump the post variables inside the saveCheckin function (use network tab to see the response of the ajax to see the var_dump), and if it's there, just keep moving up and see where the data is stopped. Commented Apr 8, 2016 at 13:31
  • @Alok What do you mean? Like if I were to change the jQuery to be something like $("#checkin").click(function(e){ and make the <input type="submit" id="saveCheckin"...> a button? No it still does not work... Same results Commented Apr 8, 2016 at 13:35
  • I mean echo something in saveCheckin() at beginning of function and exit the script. Does it get display on the screen ? Commented Apr 8, 2016 at 13:38
  • @Alok : please correct me if im wrong, but i think that is not possible since that is an AJAX call ... for owner, if i face similar problem, first step would be to check whether function saveCheckin() is really called, usually i would use file log, or may be console.log, and then check whether ur parameter to UpdateReport($data) is correct Commented Apr 8, 2016 at 13:42

1 Answer 1

1

SOLUTION

Thanks to Goose (in the comments) I was able to find my issue. I did not know this, but you can see the response from the server if you go to the developer tools, and go to the Network tab. As soon as I went there I noticed that codeigniter was throwing an Array to string conversion error in my model.

Here is what my model looks like now (the data is saving and everything):

public function UpdateReport($data)
{
    for($i=0;$i<count($data['ConditionID']);$i++) {
        $this->db->where('ConditionID',$data['ConditionID'][$i]);
        $this->db->update('Conditions', array('Status' => $data['Status'][$i], 'Description' => $data['Description'][$i]));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.