I am attempting to send an array of variables to my CakePHP action for editing purposes. The array is created from the ids of table rows. Below is the javascript code snippet that can be found in my index.ctp file.
<script type="text/javascript">
$('.edit_selected').click(function()
{
var selected = new Array();
alert('Edit Selected Has Been Clicked');
$("#[id*=LocalClocks]").each(function()
{
if(false != $(this).is(':checked'))
{
selected.push($(this).attr('id').replace('LocalClocks', ''));
$.ajax({
type: 'POST',
url: "/localhost/LocalClocks/editSelected/",
data: JSON.stringify(selected),
dataType: "json",
success: function(data){ alert(data); alert('Edit Success'); }
});
}
});
});
</script>
The logic involves extracting ids beginning with 'LocalClocks'
, removing 'LocalClock' to retain only numbers, then adding these id numbers to the selected array. The intention is to pass this array to the editSelected()
function.
As I am not knowledgeable about data posting, I have a few inquiries. Is the existing code accurate so far? Should the editSelected()
function include a parameter for the array being passed, or will the posted data automatically populate $this->request->data
?
Shown below is the editSelected
action:
public function editSelected()
{
$this->set('isEditSelValid', false);
$this->set('editSelValidationErrors', false);
if ($this->request->is('post'))
{
$localClocksEntries = $this->LocalClock->find('all');
foreach($localClocksEntries as $LocalClock)
{
$this->LocalClock->id = $LocalClock['LocalClock']['id'];
$this->LocalClock->save($this->request->data);
$this->set('isEditSelValid', true);
$this->set('editSelValidationErrors', false);
}
}
}
The purpose behind sending this data is to obtain the ids of specific rows for editing. Each row contains checkboxes that the aforementioned JavaScript is capable of extracting and compiling into an array. It is essential to iterate through this array and update the user input accordingly.