2011-06-05 99 views
0

我已經在使用javascript的電子商務網站內創建了動態定價表單,並且它工作正常。不過,我寧願將更復雜的定價計算放在HTML表單外部的PHP文件中。 計算相當複雜,我已經得到他們所有使用PHP的工作,但我是新來的PHP和我的生活無法弄清楚如何獲得計算結果返回到HTML表單。然後,用戶可以 「加入購物車」外部php表單結果

這裏是非常簡單的例子,試圖說明

HTML表單

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>demoform</title> 
</head> 

<body> 
<form id="form1" name="form1" method="post" action="myform.php"> 
<label>Width<input type="text" name="width" id="width" size="6"></label><p> 
<label>Length<input type="text" name="length" id="length" size="6" ></label><p> 
<label>Price $</label> <input id="Price" readOnly size=6 name=Price><p> 
<label for=otherprice></label><input id="otherprice" readOnly size=6 type=hidden 
name=otherprice><p> 
<input type="submit" name="submit" id="submit" value="Get Price" ><p> 
<input id="addtocart" title="Add to Cart" onclick="return validate(this.form)" value="Add to Cart" type="submit" ?> 
</form> 
</body> 
</html> 

PHP文件

<?php 

$width=$_POST['width']; 
$length=$_POST['length']; 
$Area = $width*$length; 
$total = $Area * .01; 
$price = round($total * 100)/100; 
$otherprice = round($price * 1.2 * 100)/100 ; 

?> 

<?php echo "$price"?><br/> 
<?php echo "$otherprice"?> 

它可能不是一個新的頁面打開樂 爲$價格要輸入到HTML價格字段 和$ otherprice是demoform.html頁面

約翰

+0

如果我沒有誤解你,你可以使用ajax與外部的php文件進行通信。只需在Google上搜索jquery ajax。 – SimplyZ 2011-06-05 17:12:11

回答

3

PHP腳本中輸入到HTML otherprice(隱藏)字段:

<?php 
$width =$_GET['width']; 
$length = $_GET['length']; 
$area = $width * $length; 
$total = $area * .01; 
$price = round($total * 100)/100; 
$otherprice = round($price * 1.2 * 100)/100; 

echo json_encode(array($price, $otherprice)); 
?> 

的JavaScript:

function Ajax() { 
    this.instance = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'); 
    this.request = function(url, callback) { 
     this.instance.open('GET', url, true); 
     this.instance.onreadystatechange = function() { 
      if (this.readyState == 4 && this.status == 200) { 
       callback(this.responseText); 
      } 
     } 
     this.instance.send(null); 
    } 
} 
function validate(form) { 
    (new Ajax).request('prices.php?width=' + form.width.value + '&length=' + form.length.value, function(respons) { 
     var prices = eval(respons); 
     form.price.value = prices[0]; 
     form.otherprice.value = prices[1]; 
    }); 
} 

Ajax類是我創建的。你可以自由使用它。你也可以在沒有課堂的情況下做。我通常這樣做,因爲很容易製作多個XHR對象。

HTML:

<form name="form1" method="post" action="myform.php"> 
    <p> 
     <label>Width <input type="text" name="width" size="6" /></label><br /> 
     <label>Length <input type="text" name="length" size="6" ></label><br /> 
     <label>Price <input type="text" name="price" readonly="readonly" size="6" /></label> 
     <input type="hidden" name="otherprice" readonly="readonly" size="6" /><br /> 
     <input type="button" value="Get Price" onclick="validate(this.form)" /> 
    </p> 
    <p> 
     <input type="submit" value="Add to Cart" /> 
    </p> 
</form> 

也請清理你的HTML。 :)

+0

嗨邁達斯 - 感謝您的快速回復 - 沒有能夠得到它的工作。 AJAX對我來說是全新的 - 我是否將代碼放在html文件中,因爲我會將它放在HTML中 – John 2011-06-05 17:43:02

+0

將JS放在窗體的HTML文件中。按照AJAX的要求,PHP代碼應該在'prices.php'中。 – Midas 2011-06-05 17:56:30

+0

再次感謝 - 我的不好 - 已經把它命名爲price.php - 正在填充字段但是它是零 - calcs似乎已經不合適 – John 2011-06-05 18:06:48