简体   繁体   English

循环无法与php&ajax一起正常工作

[英]Loop not working properly with php & ajax

So, basically i'm trying to create a simple usd to pounds converter using php and ajax. 因此,基本上,我正在尝试使用php和ajax创建一个简单的usdpounds转换器。 I know it'd be much easier with jQuery but this is an assignment and jQuery isn't allowed. 我知道使用jQuery会容易得多,但这是一项工作,并且不允许使用jQuery。 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 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 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 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. 我对aJax还是很陌生,因此将不胜感激。

The problem is causing because you are sending a GET http request to the server and but you are not sending the data to the server. 造成此问题的原因是,您正在将GET http请求发送到服务器,但是没有将数据发送到服务器。 You have to use query string to pass the data to the server while calling the function. 您必须在调用函数时使用查询字符串将数据传递到服务器。

In Ajax file do changes 在Ajax文件中进行更改

function submitForm() {

damount = document.getElementById("damount").value;

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

In php file now damount will be available. 现在在php文件中,damount将可用。 Here in php file $amount variable is undefined in is_numeric function. 在php文件中,$ amount变量在is_numeric函数中未定义。 Because it has defined inside the block later of condition check. 因为它在条件检查的稍后块内定义了。 So do changes 变化也是如此

<?php

if (isset($_REQUEST['damount']) && is_Numeric($_REQUEST['damount'])) 
{ 
$amount = $_REQUEST['damount'];
$rate = 0.80;
$money = $rate * $amount;
echo '£' . number_format($money, 2, '.', '');
}
 else 
{ 
echo "Please make sure entry is a valid number."; 
}
?>

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. 问题是在ajax中,您必须传递值$ _REQUEST ['damount']。在执行get请求时,应将其作为查询字符串传递。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM