I have 2 forms which performs calculations for different purposes, now I have another field in the same cshtml which is required to take the result from the above 2 forms and perform an operation in the controller. How do I achieve that. Below is my javascript which I used for forms having multiple input fields, as I said before now the issue is my 2 input fields are in 2 different forms, I need to get that and send to my controller.
The below 2 forms will perform the addition in the controller class. Now I need to consider results from the id: liability and id:totalProperty and perform another calculation at my controller.
The controller is just a post method which takes input and performs addition. Basically I need totalworth=totalProperty+liability
<script>
var request;
if (window.XMLHttpRequest) {
//New browsers.
request = new XMLHttpRequest();
var formData = new FormData(document.forms.assets); //here instead of forms name I want to pass //the id of the 2 input fields and send that for operation in the controller.
}
if (request != null) {
var url = "Home/TotalAssets";
request.open("POST", url, false);
request.send(formData)
}
totalAssets.value = request.response;
}
</script>
Below is my html
<table align="center">
<tr>
<td>
$<input type="number" id="totalworth" name="totalworth" contenteditable="false" />
</td>
</tr>
</table>
<form name="property">
<table>
<tr>
<td> Property1 </td>
<td> $<input name="property1" type="number" onchange="Assets(event)" value="2000.00" /></td>
</tr>
<tr>
<td> property2 </td>
<td> $<input type="number" onchange="Assets(event)" name="property2 " value="4000.00" /></td>
</tr>
<tr>
<td>$ <input type="number" readonly="readonly" name="totalProperty" id="totalProperty" onchange="Assetscalculate(event)"/></td>
</tr>
</table>
<form>
<form name="liabilities">
<table>
<tr>
<td><h2>Liabilities on property</h2></td>
</tr>
<tr>
<td>
<h3> Short Term Liabilities</h3>
</td>
</tr>
<tr>
<td> Credit Card 1 </td>
<td> $<input type="number" onchange="liabilityChange(event)" id="cccard1" name="cccard1" min="0" value="4342" /></td>
</tr>
<tr>
<td> Credit Care 2 </td>
<td> $<input type="number" id="cccard2" name="cccard2" min="0" value="322.020" onchange="liabilityChange(event)" /></td>
</tr>
<tr>
<td> $<input type="number" id="liability" name="liability" readonly="readonly" onchange="Liabilitiescalculate(event)" contenteditable="false" /></td>
</tr>
</form>
I think I have missed something while trying to check with your code in JS
$("#totalAssets, #liability").change(function()
{
let totalAssets = 0;
let liability = 0;
totalAssets = $('#totalAssets').val();
liability = $('#liability').val();
//When values are both set, POST both values to the server..
if (totalAssets !== '' && totalAssets !== 0 && liability !== '' && liability !== 0) {
Process(totalAssets, liability);
}
});
function Process(totalProperty, liability) {
var url = '/Home/TotalAssets?totalProperty=' + totalProperty + '&liability='
+ liability;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
var result = xmlhttp.responseText;
//Do something with the result
}
else {
alert('Something went wrong: ' + xmlhttp.status);
}
}
};
xmlhttp.open("POST", url, true);
xmlhttp.send();
} I have put above code in my javascript but it never executes the function.