idcreatv's avatar

Best practice for posting ajax 'options' from a 'show' view?

Hi, I'm after some advice about the best way to handle some Ajax requests in a project I'm working on.

In the project I need to allow the user to 'toggle' various options which are dynamically changed via ajax.

For example, I may have the following options:

Show company name [On | Off]

Show address [On | Off]

Show phone number [On | Off]

Show email address [On | Off]

How would you suggest I set this type of thing up? I was thinking of creating one column (called 'employee_options') and then populate it with an array of objects such as…

[{showCompany: true , showAddress: true, showPhone: false, showEmail: true}]

…this would mean if I need to add more options in the future I won't need to add columns etc. Would this be best practice? Also, would I run into any validation problems if I sent the array like that?

Next, would I be correct in assuming the following would be the best way to set up my table:

  1. Create new migration 'addOptionsToEmployees' and add 'employee_options' as the column.

  2. When creating a new employee I would automatically pass default options ( so this would be [{showCompany: true , showAddress: true, showPhone: false, showEmail: true}] etc) in the store part of the controller.

  3. When editing an existing employee I wouldn't want to overwrite the options already there, so should I simply leave the 'employee_options' column out of the update request or would that throw an error as nothing is being saved into the column?

Finally, this is the normal setup I have for sending data to the server:

$.ajaxSetup({
    headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }
});
$.ajax({
    type: '',
    url: '',
    success: function(returnedData){

    },
    error: function(error){

    }
});

When the user isn't adding or editing an employee and they're just changing the options using the 'show' view via toggle switches, what is the best way to send the data to the server? Should I do it as per the following…

ajax url: '/employees/options/'+employeeId+'/'+showCompany+'/'+showAddress+'/'+showPhone+'/'+showEmail,
Route::post('/employees/options/{employeeId}/{showCompany}/{showAddress}/{showPhone}/{showEmail}',['middleware'=>'check-permission:user|admin|superadmin','uses'=>'EmployeeController@employeeOptions']);

…with a controller function like:

public function employeeOptions(Request $request, $employeeId, $showCompany, $showAddress, $showPhone, $showEmail){
        //  Do stuff
    }

…or is there a more efficient way of doing it?

I'll also need to format the data in the //Do stuff part (above) before I insert it into the employee_options column; how would you suggest I format this insert statement?

Sorry for asking so many questions, I'm gradually getting to grips with Laravel but want to make sure I'm not developing poor coding skills. Hopefully other members will also find this thread of use. :)

0 likes
4 replies
EventFellows's avatar

If you use a POST request you do not need to add your placeholders into the url.

Just post to e.g. ''/employees/options" and add everthing telse to the data object you post.

You probably want to use a form for that using one <input ....> field for each of your settings and send submit it together.

idcreatv's avatar

Thanks for the pointer @EventFellows.

Ideally I was wanting to make the changes on-the-fly without needing a form to be posted. I have a controller set up for 'Employees' which handles the name, address and phone number etc but need to add a simple options panel where some of these details are turned on or off ready for when a training certificate is generated (as a PDF).

This is because if a person leaves the company the certificate can be tied to them rather than their employer, although some certificates may be non transferable so we need the option to toggle things like company name and address on or off.

I'm only just getting to grasps with Laravel and jQuery so this seems the perfect way for me to learn how to handle this sort of UI.

Further to my initial post I've added a column called 'employee_options', retrieved the column, looped through the resultant object in jQuery, and populated a list.

Its the best way to send everything back to Laravel I'm sort unsure of.

EventFellows's avatar
Level 16

An Ajax call in the background kind of is 'on the fly' without a page reload.

Alternatively create a simple ->hasMany() relationship to a separate employee_options table that would allow to eaiser query it later.

But definitively check out Jeffres mini series about user settings which is exactly your scenario:

1 like

Please or to participate in this conversation.