douhuanchi6586 2015-02-18 18:28
浏览 79

AJAX实现:未捕获的TypeError:无法读取null的属性'documentElement'

I need another set of eyes, as I'm going backwards at this point. Have php/javascript to autoupdate a 'shopping cart' as the user inputs data. However, there seems to be a conflict between codes. This is my first time implementing AJAX methods. The table that you can see in the HTML needs to allow for user input, then uses the JS to communicate with the PHP. However, at this juncture, that's not happening.

The user should see a value change 'onchange' as you can see.

Here's enough HTML to see user input/return:

<html>
<head>
<script src="PurchaseTableClientSide.js"></script>
</head>
<body>

<form>
<table border="1">
  <tr>
    <th>Item Name<br></th>
    <th>Price</th>
    <th>Quantity</th>
    <th>Total</th>
  </tr>
  <tr>
    <td>CD 1<br></td>
    <td>$12.50</td>
    <td><input type="number" id="quantityCD1" step="1" min="0" onchange="calculatePrice()"></td>
    <td id="totalCD1">0.00</td>
  </tr>

Here's the PHP:

<?php
error_reporting(0);

//variable to store the result.
$xml = new SimpleXMLElement("<response/>");

if(isset($_GET['qCD1']) && isset($_GET['qCD2']) && isset($_GET['qMVA'])) {
    $totalCD1 = 12.50 * $_GET['qCD1'];
    $totalCD2 = 12.95 * $_GET['qCD2'];
    $totalMVA = 19.95 * $_GET['qMVA'];
    $subtotal = $totalCD1 + $totalCD2 + $totalMVA;
    $shipping = $subtotal * 0.5;
    $total = $subtotal + $shipping;

    //http://php.net/manual/en/simplexmlelement.addchild.php
    $xml->addChild('totalCD1', $totalCD1);
    $xml->addChild('totalCD2', $totalCD2);
    $xml->addChild('totalMVA', $totalMVA);
    $xml->addChild('subtotal', $subtotal);
    $xml->addChild('shipping', $shipping);
    $xml->addChild('total', $total);
    $xml->addChild('error', False);

} else {
    //Inform the client that there was an error.
    //Currently not used, but sounds like a cool idea.
    $xml->addChild('error', True);
}

//Define the content as being XML response
header("Content-Type:text/xml");
//Send it back to the user
echo $xml->asXML();

?>

And here is the javascript:

 

//Global variable for the AJAX connection.
var xmlHttp;

function calculatePrice()
{
    var quantityCD1 = document.getElementById("quantityCD1").value;
    var quantityCD2 = document.getElementById("quantityCD2").value;
    var quantityMovieA = document.getElementById("quantityMovieA").value;
    
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    }
    
    var url="purchaseTable.php";
    url=url+"?qCD1="+quantityCD1;
    url=url+"&qCD2="+quantityCD2;
    url=url+"&qMVA="+quantityMovieA;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function stateChanged()
{
    //Check to make sure the response is good.
    if (xmlHttp.readyState==4) {
        
        //Get an XML parser out of it.
        var xmlDoc = xmlHttp.responseXML.documentElement;
        
        //Pull the data from the XML response and put it in the HTML.
        //http://www.ajaxtutorial.net/index.php/2006/02/28/ajax-with-php-using-responsexml/
        document.getElementById("totalCD1").innerHTML = xmlDoc.getElementsByTagName('totalCD1')[0].firstChild.nodeValue;
        document.getElementById("totalCD2").innerHTML = xmlDoc.getElementsByTagName('totalCD2')[0].firstChild.nodeValue;
        document.getElementById("totalMVA").innerHTML = xmlDoc.getElementsByTagName('totalMVA')[0].firstChild.nodeValue;
        document.getElementById("shipping").innerHTML = xmlDoc.getElementsByTagName('shipping')[0].firstChild.nodeValue;
        document.getElementById("total").innerHTML = xmlDoc.getElementsByTagName('total')[0].firstChild.nodeValue;
    }
}

//Connect to the PHP server.
function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看