weixin_33725722 2017-06-08 12:44 采纳率: 0%
浏览 63

ajax在localhost上不起作用

I have a simple Ajax calculation but it doesn't work

that's the JavaScript Code

<script>


   function showFees() {
        e.preventDefault();
        var weight = ('weight').val;
        var ship_type== ('ship_type').val;
        var eol== ('eol').val;
        if (weight == 0) {
            document.getElementById("txtHint").innerHTML = "";
            return;
        } else {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
         document.getElementById("txtHint").innerHTML =this.responseText;
                }
            };
            xmlhttp.open("GET", "sdr.php?weight=" + weight "ship_type" + ship_type + "eol" + eol, true);
            xmlhttp.send();
        }
    }

that's my form

<form>

 <label>weight:</label> 

 <input type="number" name="weight" min="0" value="0">

 Ship Type:

 <select name="ship_type">

        <option value="1">Tankers of Crude Oil</option>
        <option value="2">Tankers of Petroleum Products</option>

      </select>
      Laden Or Ballast: 
      <select name="eol">
                <option value="0">Ballast</option>
                <option value="1">Laden</option>
              </select>   
      <input type="submit" onclick="showFees()" value="calc">
      </form>
      <p>Suggestions: <span id="txtHint"></span></p>

and my sdr page to calculate the final result

$weight;
$i=0;
$wpt=0/*weight per ton result*/;
$ship_type;
$eol;/*empty or loaded*/
$channel_weights=array(5,5,10,20,30,50,10000);

$weight = $_REQUEST['weight'];
$ship_type = $_REQUEST['ship_type'];
$eol = $_REQUEST['eol'];
while($weight!=0){
    if($weight>=$channel_weights[$i]){
        $wpt+=$channel_weights[$i]*$ship_load_vlues[$eol][$ship_type][$i];
        $weight-=$channel_weights[$i];
    } 
    else{
        $wpt+=$weight*$ship_load_vlues[$eol][$ship_type][$i];
        $weight=0;
    }

    $i++;
}

$final = $wpt*$float_sdr*1000;
echo $final === "0" ? "wrong data" : $final;
 ?>

I think echo must return to span with ID 'txtHint' but it doesn't return any and if I tried to put alert in java script doesn't work either and I tried to add ajax library and doesn't work also

  • 写回答

3条回答 默认 最新

  • csdnceshi62 2017-06-08 12:47
    关注

    You're using e.preventDefault(); but you don't have e defined at this point. Pass it as an argument on the button:

     onclick="showFees(event)"
    

    Capture it in the function:

    function showFees(e) {
    

    Fix the bad URL format and use encodeURIComponent on the values:

    xmlhttp.open("GET", "sdr.php?weight=" + encodeURIComponent(weight) + "&ship_type" + encodeURIComponent(ship_type) + "&eol" + encodeURIComponent(eol), true);
    

    Fix these two lines which are nowhere near correct:

    var ship_type== ('ship_type').val;
    var eol== ('eol').val;
    

    Should be:

    var ship_type = document.querySelector('select[name=ship_type]').value;
    var eol = document.querySelector('select[name=eol]').value;
    

    Open the browser dev tools (F12) and look at the console, these issues would be producing errors which would help you to narrow the problems down.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题