It seems like the issue you're facing is related to the form data not being updated with the JavaScript changes before the form is submitted. To ensure that the updated values are sent to the server, you need to make sure that the form inputs are correctly updated by your JavaScript code.
From your description, it looks like you're updating the value of a hidden input with the ID services_hidden and a text input with the ID quote_services. However, in your controller, you're trying to retrieve the value from services_hidden.
First, ensure that your JavaScript code is correctly updating the value of the hidden input. There's a typo in your JS snippet where you're assigning serviceTotal instead of servicesTotal. Here's the corrected JS snippet:
if ((cost > 0) && (servicesTotal >= cost)) {
servicesTotal = servicesTotal - cost; // Corrected variable name
$("#services_hidden").val(servicesTotal.toFixed(2)); // Update the hidden input with the fixed value
var serviceTotalFixed = servicesTotal.toFixed(2);
$("#quote_services").val(serviceTotalFixed); // Update the text input
}
Next, ensure that the form containing these inputs is correctly submitted. If you're using AJAX to submit the form, make sure to serialize the form data or construct the data object with the updated values.
If you're submitting the form traditionally (without AJAX), the updated values should be sent as long as the inputs are within the <form> tags and have the name attribute set. For example:
<form method="post" action="/your-submit-route">
<!-- Other form fields -->
<input type="hidden" name="services_hidden" id="services_hidden" value="{{ $quotation->services_total }}">
<!-- Make sure to include CSRF token for Laravel forms -->
@csrf
<!-- Submit button -->
<button type="submit">Submit</button>
</form>
Finally, in your controller, you're correctly retrieving the value from the request with $request->get('services_hidden');. Just make sure that the form is being submitted with the updated values.
If you're still facing issues, check the following:
- Ensure that the JavaScript code is executed before the form is submitted.
- Verify that the form has the correct
nameattributes corresponding to the data you want to send. - Check that the form does not have any other inputs with the same
nameattribute that could be overriding the values. - If you're using AJAX, ensure that the data object includes the updated values and that the request is sent to the correct route.
By following these steps, you should be able to ensure that the updated JavaScript values are sent to the server when the form is submitted.