user2970017
user2970017

Reputation: 11

Saving changes to a dropdown box into a database in CakePHP

I am new to cake and mysql, and am trying to create a simple job tracking app. I want to have a dropdown box for each job with a list of the status' a job can be at. When a user changes the active item in the box I want to save this into the database.

Any help in how to handle this would be very much appreciated. Below is what I have tried so far:

How I create the set of forms in the view with the options taken from the enums in my database table:

<?php $id = count($jobs)-1; ?>
<?php for ($job = count($jobs)-1; $job >= 0; --$job): ?>
<tr>
<td>
<?php echo $this->Form->input('status'.(string)$id, array('type'=>'select', 'class' => 'statusSelect','label'=>'', 'options'=>$states, 'default'=>$jobs[$job]['Job']['Status'])); ?>
</td> 

I am using a jquery script to set an on change listener for each dropdown and call an action in my controller:

$(".statusSelect").change(function(){
//Grab job number from the id of select box
    var jobNo = parseInt($(this).attr('id').substring(6));
var value = $(this).val();
    $.ajax({
        type:"POST",
        url:'http://localhost/projectManager/jobs',
        data:{  'id': jobNo,
            'status':value}, 
            success : function(data) {
               alert(jobNo);// this alert works
            },
        error : function() {
           //alert("false");
        }
    });

});

And I have this function in my controller:

public function changeState($id = null, $status = null) {
    //I don't think the id and status are actually 
        //being placed as arguments to this function
        //from my js script
}

Thank you!!!

Upvotes: 1

Views: 353

Answers (1)

iso27002
iso27002

Reputation: 179

You are POSTing to /projectManager/jobs, which corresponds to ProjectManagerController::jobs().

Your function is declared as public function changeState($id = null, $status = null). Assuming changeState(..) is a function within ProjectManagerController, this corresponds to /projectManager/changeState/$id/$status.

You need to switch the URL the AJAX is POSTing to. You can either do something like:

url:'http://localhost/projectManager/changeState/'+jobNo+'/'+value', remove the data {} and leave your function as is, or you can do

url:'http://localhost/projectManager/changeState', leave the data {}, change your function to changeState() and then use $this->request->data within changeState() to access the data.

I am guessing you have another function, jobs(), and that is why the AJAX is working properly and the alert is generating.

Upvotes: 2

Related Questions