I have the following query: I want to know if it is possible to use a variable in an attribute in ajax, I do not know if I explain myself, but in the following code I will try to explain:
<script>
function active_change_update(ruta, id, status, campo)
{
if(status==1 || status==null){
var estado = 0;
}else{
var estado = 1;
}
var dato = {
'id': id,
'_token': "{{ csrf_token() }}",
//por defecto utilizaba active ya que en mi tabla producto esta asi y lo unico que quiero modificar es el valor del active
//hasta aqui todo bien
'active': estado
};
$.ajax({
type: "PUT",
url: ruta,
dataType: 'json',
data: dato,
success:function(data) {
window.location.reload();
}
});
}
As you can see I want to modify the value of the active, but I realized that this function can be used to modify several attributes of other tables, for example the "sold" or "delivered" that are also Boolean. What I want to do is the following:
function active_change_update(ruta, id, status, campo)
{
if(status==1 || status==null){
var estado = 0;
}else{
var estado = 1;
}
//recibo el valor de "campo" que en este caso es "vendido" y quiero pasar ese atributo al json
var dato = {
'id': id,
'_token': "{{ csrf_token() }}",
//de esta manera
campo: estado
};
$.ajax({
type: "PUT",
url: ruta,
dataType: 'json',
data: dato,
success:function(data) {
window.location.reload();
}
});
}
Absolutely this is not the right way and I have looked for several alternatives, I would be very grateful if you could help me with this. Thank you very much already.