如何将Span Id值转换为PHP变量?

时间:2015-08-02 21:06:31

标签: javascript php jquery html

我有点混淆如何将Span ID值转换为PHP变量。当我尝试DOM时,Span变得不起作用。 我想存储Span id“sum”的值。 请帮助我。

HTMl + PHP表单:

<div class="form-sep">
  <div class="row-fluid">
    <div class="span6">
      <label class="field-title">Fees Particular [A] + [B] = [Payble Fees]</label>
      <input type="text" name="valuesum" class="valuesum" value="<?php $mf = mysqli_fetch_array(mysqli_query($conn,"SELECT * FROM total_fees where class = '$qry[class]'")); $m_f = $mf['total_fees']/10; echo $m_f;?>" > +
      <input type="text" name="valuesum" class="valuesum" value="<?php echo $i; ?>" > =
      <td align="center"><strong>Rs. <span id="sum"></span></strong></td>
    </div>
  </div>
</div>

脚本:

<script>
    $(document).ready(function(){

        //iterate through each textboxes and add keyup
        //handler to trigger sum event
        $(".valuesum").each(function() {

            $(this).keyup(function(){
                calculateSum();
            });
        });

    });

    function calculateSum() {

        var sum = 0;
        //iterate through each textboxes and add the values
        $(".valuesum").each(function() {

            //add only if the value is number
            if(!isNaN(this.value) && this.value.length!=0) {
                sum += parseFloat(this.value);
            }

        });
        //.toFixed() method will roundoff the final sum to 2 decimal places
        $("#sum").html(sum.toFixed(2));
    }
</script>

1 个答案:

答案 0 :(得分:1)

你的HTML中有问题,jQuery工作正常。

Updated Fiddle

<div class="form-sep">
    <div class="row-fluid">
        <div class="span6">
            <label class="field-title">Fees Particular [A] + [B] = [Payble Fees]</label>
            <input type="text" name="valuesum" class="valuesum" value="<?php $mf = mysqli_fetch_array(mysqli_query($conn,"SELECT * FROM total_fees where class = '$qry[class]'")); $m_f = $mf['total_fees']/10; echo $m_f;?>" > +
            <input type="text" name="valuesum" class="valuesum" value="<?php echo $i; ?>" > =
            <td align="center"><strong>Rs. <span id="sum"></span></strong></td>
        </div>
    </div>
</div>

你不能在input内运行mysql查询,正确的做法是,首先运行查询

<?php $mf = mysqli_fetch_array(mysqli_query($conn,"SELECT * FROM total_fees where class = '$qry[class]'"));
$m_f = $mf['total_fees']/10;
?>

然后在input中将变量放入value

<input type="text" name="valuesum" class="valuesum" value="<?php echo $m_f;?>" >

要存储值,首先必须将其存储到输入中(检查更新的小提琴)

<input type="text" name="total" id="total" value="" >

然后将其与表单一起发布并存储到数据库中,