dtz8044 2013-09-27 07:55
浏览 108

将数组数组添加到html表单输入中并显示计算结果

I have an array having 2-3 titles. eg. 'Get 20% Off' , 'Receive 40% Savings' , 'Grab 10% discounts' etc. I would like to get the numeric value before '%' sign. and pass this numeric value into a html form as inputs. and in return the form would calculate a percentage amount. and display all the results.

Right now i have managed to make this work for a single title. but i am not able to get a array of titles.

my code is as follows -

//code for getting all the titles //putting it into an array called '$array'

foreach($array as $str) {
if(preg_match('/(\d+)\%/i' ,$str,$m)) {
             echo $m[1],"
"; ?>

<?php  }
} ?>

//the above code extracts the amount before the percentage off sign. and the below code passes it as a input to the form (name=a). and other input (name=b) is coming from a different source. thats working fine. for calculation consider that input to be 100. I would need to calculate the percent value w.r.t 100. for eg- if input a is 5, then another input b is 100. the value to be outputted would be 5.

<form name="form1">
  <input type="text" name="a" value="<?php echo $m[1]; ?>" size=5> 
  <input class="budget-sidebar" type="text" name="b" id="amount" size=5>

the below html is used for displaying the calculated value and submitting the form.

<input type="text" name="total1" value="ans1" size=5 maxlength=4>
  <input type="button" value="Calculate" onClick="calc1(this.form)">
  </form>

the below javascript function is responsible to take the inputs and calculate the value and displaying it.

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function calc1(form) {
a = form.a.value/100;
b = a*form.b.value;
form.total1.value = b;
}
//  End -->
</script>

i have posted a similar question requesting for different suggestions & this is supposed to be a different query. thanks.

  • 写回答

1条回答 默认 最新

  • dtgr3392 2013-09-27 08:12
    关注

    try to convert form value to integer before performing calculation

    function calc1(form) {
        a = parseInt(form.a.value, 10)/100;
        b = a* parseInt(form.b.value, 10);
        form.total1.value = b;
    }
    

    And you will have to round the value to avoid to get strange value

    [Edit]:

    And to get multi string some thing like this will may work (no test sorry)

    // php
    $values = array();
    foreach($array as $str) {
        if(preg_match('/(\d+)\%/i' ,$str,$m)) {
            $values[] = $m[1];
        }
    }
    
    // for html
    <form name="form1">
        <?php for($i = 0; $i < count($values); $i++) { ?>
        <input type="text" name="a<?php echo $i ?>" value="<?php echo $values[i]; ?>" size=5> 
        <?php} ?>
        <input class="budget-sidebar" type="text" name="b" id="amount" size=5>
    
    // javascript
    function calc1(form) {
        var i = 0, a = 0;
        while (form["a" + i]) {
            a += parseInt(form["a" + i].value, 10);
            i++;
        }
        b = a/100 * parseInt(form.b.value, 10);
        form.total1.value = b;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据