简体   繁体   中英

How to get Span Id value into PHP Variable?

I am bit confuse how to get Span ID value into PHP Variable. When I tried DOM, Span become will not work. I want to store the value of Span id "sum". Please help me in this.

HTMl + PHP Form:

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

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

You have problem here in your HTML, jQuery works fine.

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>

You can't run mysql query inside input , right way to do it is, first run query

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

and then in input put variable in value

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

And to store values, first you have to store it into input (check updated fiddle)

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

and then post it with form and store into database,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM