本文介绍了的PHP:onchange选项选择总更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   <?php
    $price = 85;
    echo"
    <script type=\"text/javascript\">
    function updateHarga(){
        var input = document.getElementById(\"shipping\");
        var dropd = document.getElementById(\"dd\");
        input.value = parseInt(dropd.value);
    };
    </script>";
    $shipping ="<input type=\"text\" id=\"shipping\" value=\"$price\">";
    $total = $price+$shipping;
    echo "
    $total
        <br>
        Pilih Frame: <select name=\"$fr\" id=\"dd\" onChange=\"updateHarga()\">
        <option value=\"0\">Economy</option>
        <option value=\"10\">DHL +$10</option>
        <option value=\"15\">FEDEX +$15</option>
    </select>
    ";
    ?>

如何从$ price + $ shipping中更新$ total计算.$ price是来自mysql的查询,而$ shipping是来自选项select的.在此之前,我使用javascript并正常工作.但我想计算并显示为变量$ total.问题出在$ shipping中,也许问题出在

How to update $total calculate from $price + $shipping.$price is query from mysql and $shipping from option select.before this i use javascript and worked. but i want calculate and display to variable $total. the problem is in $shipping, maybe problem is in the

$shipping ="<input type=\"text\" id=\"shipping\" value=\"$price\">";

因为不是字符串格式.还有其他方法可以得到相同的结果吗?

because not string format.is there any other ways to get the same results?

谢谢.

推荐答案

此行$total = $price+$shipping;似乎多余.您正在将字符串添加到整数.字符串$shipping包含一个表单输入框,其值已经为$price.这样,在其余代码示例中的任何地方都不会使用$total.

This line $total = $price+$shipping; appears redundant. You are adding a string to an integer. The string $shipping contains a form input box with the value of $price already. $total is then not used any where in the rest of your code sample.

更新1

不确定我能完全理解,但是请尝试以下操作:

Not entirely sure I understand, but try this:

<?php
    $price = 85; //default shipping rate
    echo"
        <script type=\"text/javascript\">
            function updateHarga(){
                var input = document.getElementById(\"shipping\");
                var dropd = document.getElementById(\"dd\");
                var base_price = $price;
                input.value = parseInt(dropd.value) + base_price;
            };
        </script>
        <input type=\"text\" id=\"shipping\" value=\"$price\">
        <br>
        Pilih Frame: <select name=\"$fr\" id=\"dd\" onChange=\"updateHarga()\">
        <option value=\"0\">Economy</option>
        <option value=\"10\">DHL +$10</option>
        <option value=\"15\">FEDEX +$15</option>
    </select>
    ";
?>

这篇关于的PHP:onchange选项选择总更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 10:55