0

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;
}

?>

Home Page of the Calculator

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.

  • Are you making sure there are no errors before you calculate the mortgage? – aynber Jul 03 '19 at 17:31
  • @aynber I changed the code to look like this: `if ($error_cnt > 0) { print $msg; } else { $interestrate_forcalc = $interestrate / 100 ; $monthly_payment = ($amountfinanced * $interestrate_forcalc) / 12; $monthly_payment_formatted = number_format($monthly_payment, 2); print $monthly_payment_formatted; }` But now it says "ERROR ON DATABASE". – Caleb Leavesley Jul 03 '19 at 19:03
  • `ERROR ON DATABASE` is not shown anywhere in your code here, so you'll have to track down where that's coming from. – aynber Jul 03 '19 at 19:06
  • Thank you! I found it and fixed it a little bit ago. Thank you for your help! – Caleb Leavesley Jul 05 '19 at 11:34

1 Answers1

0

I have solved the issue. I changed the section that said:

$interestrate_forcalc = $interestrate / 100 ;

$monthy_payment = ($amountfinanced * $interestrate_forcalc) / 12;

$monthy_payment_formatted = number_format($monthy_payment, 2);

and

if ($error_cnt > 0)
{
    print $msg;
} else {
    print $monthy_payment_formatted;
}

?>

and I changed it to say

if ($error_cnt > 0)
{
    echo $msg;
} else {
    $interestrate_forcalc = $interestrate / 100 ;

    $monthly_payment = ($amountfinanced * $interestrate_forcalc) / 12;

    $monthly_payment_formatted = number_format($monthly_payment, 2);

    echo $monthly_payment_formatted;
}

so that I can check if there is an error before calculating (like aynber said).

Then, I went to a part in my code that said:

    function processData(returnedData)
    {

      if (returnedData.substr(0,5) == 'ERROR')
      {
        var errorDiv = document.getElementById('errordiv');
        errorDiv.innerHTML = "ERROR ON DATABASE";
      } else {
        monthlypayment = document.getElementById('monthlypayment');
        monthlypayment.value = returnedData;
      }

      return false;
    }

which is where it takes the data and checks to see if there is an error, and I changed it to:

function processData(returnedData)
    {

        if (returnedData.substr(0,5) == 'ERROR')
        {
            var errorDiv = document.getElementById('errordiv');
            errorDiv.innerHTML = returnedData;
        } else {
            monthlypayment = document.getElementById('monthlypayment');
            monthlypayment.value = returnedData;

            var errorDiv = document.getElementById('errordiv');
            errorDiv.innerHTML = "";
        }

        return false;
    }

Now it properly displays the error messages.

Thank you!