dpyic24480 2017-12-14 04:15
浏览 26

使用php和ajax循环不正常

So, basically i'm trying to create a simple usd to pounds converter using php and ajax. I know it'd be much easier with jQuery but this is an assignment and jQuery isn't allowed. Below is the code i'm working on, but when I run it gives me "Please make sure entry is a valid number." even when i'm definitely putting in a numeric value. I've been searching for hours to try to find out how to fix this code, so i'm hoping someone here can give me some insight.

HTML

  <form method="get" action="">
            <label for="damount">Dollars to Convert:</label>
             <input type="text" name="damount" id="damount">

       <div>
       <input type="button"value="go" onclick="submitForm();">
       </div>
<p id="results"></p>
</div>

     </form>

AJAX

     var http2 = createRequestObject();
  function createRequestObject() {
      var ro;
      var browser = navigator.appName;
      if(browser == "Microsoft Internet Explorer"){
          ro = new ActiveXObject("Microsoft.XMLHTTP");
      }else{
          ro = new XMLHttpRequest();
      }
      return ro;
  }

  function submitForm() {

     http2.open('get', 'calculations.php');
     http2.onreadystatechange = toCalc;
     http2.send(null);
  }


  function toCalc() {
    if(http2.readyState == 4){
        document.getElementById("results").innerHTML = http2.responseText;
    }
  }

PHP

if (isset($_REQUEST['damount']) && is_Numeric($amount))
 {
    $rate = 0.80;
    $amount = $_REQUEST['damount'];
    $money = $rate * $amount;
    echo '£' . number_format($money, 2, '.', '');
}

else
{
echo "Please make sure entry is a valid number.";
}

I'm still really new to aJax, so any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • dpv50040 2017-12-14 05:00
    关注

    The problem is in ajax you have to pass the value $_REQUEST['damount'].As you are doing a get request you should pass it as query string.

     function submitForm() {
          var amount = document.getElementById("damount‌​").value;
          http2.open('get', 'calculations.php?damount=' + amount);
          http2.onreadystatechange = toCalc;
          http2.send(null);
        }
    
    评论

报告相同问题?