index.php
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>商品价值计较</title> <style type="text/css"> table { border-collapse: collapse; } tr { text-align: center; } .a4 { text-align: right; /* padding-right: 15px; */ } #myDiv { color: red; } input { border: 0; } </style> </head> <body> <form action="data.php" method="get"> <table bordercolor="#00CCCC" cellpadding="20"> <tr> <th>商品名称</th> <th>购置数量(斤)</th> <th>商品价值(元/斤)</th> </tr> <tr> <td>香蕉</td> <td><input type="text" value="0" onchange="zongji()" /></td> <td>8</td> </tr> <tr> <td>苹果</td> <td><input type="text" value="0" onchange="zongji()" /></td> <td>5</td> </tr> <tr> <td>橘子</td> <td><input type="text" value="0" onchange="zongji()" /></td> <td>7</td> </tr> <tr> <td colspan="3">商品折扣:<span>0.8</span></td> </tr> <tr> <td colspan="3"> <div>打折后购置商品总价值: 元</div> </td> </tr> </table> </form> <script> function zongji() { var b1 = document.getElementById("n1").value; var b2 = document.getElementById("n2").value; var b3 = document.getElementById("n3").value; //1.建设工具 var xmlhttp; if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari 欣赏器执行代码 xmlhttp = new XMLHttpRequest(); } else { // IE6, IE5 欣赏器执行代码 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } //2.判定工具是否筹备停当 xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("jiage").innerHTML = xmlhttp.responseText; } }; //3.发出请求 xmlhttp.open( "GET", "demo.php?c1=" + b1 + "&c2=" + b2 + "&c3=" + b3, true ); xmlhttp.send(); } </script> </body> </html>
data.php
<p> <?php $d1 = $_GET["c1"]; $d2 = $_GET["c2"]; $d3 = $_GET["c3"]; $sum = (intval($d1) * 8 + intval($d2) * +intval($d3) * 7) * 0.8; // $sum=$a1*7.99+$a2*6.89+$a3*3.99; echo "打折后购置商品总价值: $sum 元"; ?> </p>