It seems like you're trying to send an AJAX request to your Laravel controller but are unsure if the request is reaching the controller or if the data is being processed correctly. Here are a few steps to troubleshoot your code:
-
Check CSRF Token: AJAX requests in Laravel require a CSRF token to be sent with the request for security reasons. Make sure you're including the CSRF token in your AJAX request.
-
Use the correct URL: Ensure that the URL you're posting to in your AJAX request matches the route defined in your
web.php. -
Check Network Requests: Use your browser's developer tools to check the network requests being made when you change the radio button. Look for the request to
/myfireops/update-user-subscriptionor/myfireops/delete-user-subscriptionand check the response. -
Check Laravel Logs: If the request is being made but not processed correctly, check the Laravel logs in
storage/logs/laravel.logfor any errors. -
Add error handling to AJAX: Add error handling to your AJAX requests to log any potential errors that occur during the request.
Here's an updated version of your AJAX code with CSRF token inclusion and error handling:
$(document).ready(function() {
$('input[type="radio"]').change(function() {
var notifierId = $(this).closest('form').find('input[name="notifier_id"]').val();
var type = $(this).val();
var token = $('input[name="_token"]').val(); // Get the CSRF token
if (type === 'on' || type === 'cr') {
// Send AJAX request to update the subscription
$.ajax({
url: '/myfireops/update-user-subscription',
type: 'POST',
data: {
_token: token, // Include the CSRF token
notifier_id: notifierId,
type: type
},
success: function(data) {
console.log(data.message);
},
error: function(xhr, status, error) {
console.error("Error occurred: " + xhr.responseText);
}
});
} else if (type === 'off') {
// Send AJAX request to delete the subscription
$.ajax({
url: '/myfireops/delete-user-subscription',
type: 'POST',
data: {
_token: token, // Include the CSRF token
notifier_id: notifierId
},
success: function(data) {
console.log(data.message);
},
error: function(xhr, status, error) {
console.error("Error occurred: " + xhr.responseText);
}
});
}
});
});
Make sure to replace the url in the AJAX request with the correct URL if your application is not at the root of the domain.
By following these steps, you should be able to troubleshoot the issue and determine where the problem lies. If you find that the request is being made correctly and the controller method is being hit, but the data is still not being saved, you may need to debug your controller method to ensure that the data is being processed and saved correctly.