How to change the field content server side when another field is changed #1223
Replies: 2 comments 1 reply
-
To change the content of a field server-side when another field is changed in Backpack, you can use the CrudField JavaScript API along with an AJAX call to update the field content. Here's how you can achieve this:
crud.field('field_to_watch').onChange(function(field) {
// Make an AJAX call to your server
$.ajax({
url: '/your-endpoint',
type: 'GET',
data: { value: field.value },
success: function(response) {
// Update the target field with the response
crud.field('target_field').input.value = response.newValue;
}
});
}).change();
Widget::add()->type('script')->content('assets/js/your-custom-script.js');
This approach allows you to update field content dynamically based on user input, with the actual logic happening on the server-side. For more complex scenarios or if you need to update multiple fields, you might want to consider using Livewire, which will be officially supported in Backpack v12 and can handle more complex back-and-forth AJAX requests seamlessly. Remember to adjust the code to fit your specific field names and endpoint URLs. CrudField JavaScript API Relevant sources: CrudField JavaScript Library|(9) When a text input is written into, write in a second input (eg. slug) |
Beta Was this translation helpful? Give feedback.
-
Hey @Mte90 Try this tutorial, How to access CRUD fields via javascript to manipulate Form |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have various fields define in the setup method but I need that one when the value is changed in another one updates the content a field.
Beta Was this translation helpful? Give feedback.
All reactions