dongqiancui9194 2011-06-05 17:00
浏览 28
已采纳

外部php表单结果

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

  • 写回答

1条回答 默认 最新

  • dongyixiu3110 2011-06-05 17:22
    关注

    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. :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器