I am trying to validate an HTML form, and print out an error if the submissions are formatted wrong. But, instead of printing out an error below it all, it prints a very long message directly into the form.
I've tried reformatting almost everything and looking for errors, but so far I've found nothing.
In assignment_8_mortgaga_calc.php. This is the function that calls the other file.
function calcMortgage()
{
if(XMLHttpRequestObject) {
XMLHttpRequestObject.open("POST", "assignment_8_mortgage_ajax.php");
XMLHttpRequestObject.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
var returnedData = XMLHttpRequestObject.responseText;
processData(returnedData);
}
}
var amountfinanced = document.getElementById('amountfinanced').value;
var interestrate = document.getElementById('interestrate').value;
var data = amountfinanced + '|' + interestrate + '|';
XMLHttpRequestObject.send("data=" + data);
}
return false;
}
This is the div
under the form that is used to display the errors (theoretically)
<div id="errordiv" style="color: red;"></div>
Receiving data from the HTML form in the other file (assignment_8_mortgage_ajax.php)
<?php
$myData = $_POST['data']; //This receives the data passed from the HTML form
list($amountfinanced, $interestrate) = explode('|', $myData);
Doing the validations:
//Do Validations
$msg = "ERROR: ";
$error_cnt = 0;
if (empty($amountfinanced))
{
$msg .= "<br><span class='errormsg'>Please enter an amount </span>";
$error_cnt++;
} else {
if (!is_numeric($amountfinanced))
{
$msg .= "<br><span class='errormsg'>Amount entered, '".$amountfinanced."' is not numeric </span>";
$error_cnt++;
}
}
if (empty($interestrate))
{
$msg .= "<br><span class='errormsg'>Please enter an interest rate </span>";
$error_cnt++;
} else {
if (!is_numeric($interestrate))
{
$msg .= "<br><span class='errormsg'>Interest rate entered, '".$interestrate."' is not numeric </span>";
$error_cnt++;
}
}
Calculating Mortgage:
$interestrate_forcalc = $interestrate / 100 ;
$monthy_payment = ($amountfinanced * $interestrate_forcalc) / 12;
$monthy_payment_formatted = number_format($monthy_payment, 2);
And displaying the page:
if ($error_cnt > 0)
{
print $msg;
} else {
print $monthy_payment_formatted;
}
?>
With Calculated Monthly Payment
When Clicked With Empty Fields
Here is the message that shows up in the little box: Error Message
Sorry that I had to include an image only of the error message. When I pasted it into the text box, it wouldn't all show up. I expected it to show up in a div
under the "Calculate Payment" button.
Any help would be very much appreciated. Thank you in advance.