开发者

external php form results

I have created dynamic pricing开发者_高级运维 forms within a e-commerce site using javascript and it works fine. However I would prefer my more complex pricing computations to be in a PHP file external to the HTML form. The calculations are fairly complex and I have got them all working using PHP however I am new to PHP and for the life of me cant figure out how to get the result of the calculations to be returned to the html form. The user can then "Add to Cart"

Here are very simple examples to try and explain

HTML form

<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 File

<?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"?>

Instead of a new page opening is it possible for $price to be input into the HTML price field and for $otherprice to be input into the HTML otherprice (hidden) field within the demoform.html page

John


PHP script:

<?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];
    });
}

The Ajax class is something that I created. You are free to use it or not. You can also do it without the class. I usually do it this way because it's easy to make multiple XHR objects.

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>

Also please clean up your HTML. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜