0

I created a loop in my PHP file that generates 10 rows. Each row has four boxes and below is the grand total.

The scenario is, when the user enters a qty and a price, the total will automatically change using jQuery and the grand total also. And the same with row2. My problem is, I can't access the id from my PHP input fields.

Here's what I did:

for($i = 1; $i < 11; $i++){
    echo "<!-- ITEM {$i} -->";
    echo "<tr>";
       echo "<td><input type='text' name='qty{$i}' class='k-textbox' id='qty{$i}'></td>";
       echo "<td><input type='text' name='item{$i}' class='k-textbox' id='item{$i}'></td>";
       echo "<td><input type='text' name='price{$i}' class='k-textbox' id='price{$i}'></td>";
       echo "<td><input type='text' name='total{$i}' class='k-textbox' id='total{$i}'></td>";
    echo "<tr>";
}
<tr>
   <td>GRAND TOTAL</td>
   <td><input type='text' name='gtotal' value='0.00' id='gtotal' class='k-textbox' />
</tr>

Here's my jQuery code:

var x = 0;

for(x = 1; x < 11; x++){
  $('#qty'+x, '#price'+x).on('input',function() {
     var qty = parseInt($('#qty'+x).val());
     var price = parseFloat($('#price'+x).val());
      $('#total'+x).val((qty * price ? qty * price : 0).toFixed(2));
  });
}

That's all. I hope you can help me.

5
  • And your question is...? Commented Aug 15, 2013 at 3:18
  • I'm so confused... Why do you need PHP to access the ID from the input fields anyways? Isn't he PHP the one defining the IDs? Commented Aug 15, 2013 at 3:19
  • The question is very simply there is a much better way to do this Commented Aug 15, 2013 at 3:21
  • I need the ID of the inputs for my jquery. Are there any other way to perform this logic? Commented Aug 15, 2013 at 3:23
  • there you go check my answer, its less messy and no javascript loops Commented Aug 15, 2013 at 3:32

3 Answers 3

1

There isn't input event.

It's easy to get id attribute, what you really need to do is to add $(documnet).ready(..) and replace input event to change event.

So, update js code:

UPDATE: I mistook to use # selector , and have updated it to [id^=...]

$(document).ready(function(){
    // well,$('selector1,selector2')  not  $('selector1','selector2')
    $('[id^=qty] , [id^=price]').on('change',function() {
        var index=this.id.match(/\d+/)[0];
        var qty = parseInt($('#qty'+index).val());
        var price = parseFloat($('#price'+index).val());
        $('#total'+index).val((qty * price ? qty * price : 0).toFixed(2));
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

@user2585508 That's because I made a mistake, try to use parseInt but got NaN.I have replaced parseInt to regular expression.I think it works.Have a try.
still not working. do you have an email so that i can send you my whole code? and please tell me what's y error.
@user2585508 [email protected]
0

I may not be understanding the question correctly but why not have PHP calculate the grand total and then place the value in the grand total?

1 Comment

Because i want to do it in a my client side.
0

first you generate your html as follows, lets use your class to attach the trigger, and define a custom type called data-type to test against and also add data-row to keep track of the row your in.

for($i = 1; $i < 11; $i++){
    echo "<tr>";
       echo "<td><input type='text' data-row='{$i}' data-type='qty' name='qty{$i}' class='k-textbox' id='qty{$i}'></td>";
       echo "<td><input type='text' data-row='{$i}' data-type='item' name='item{$i}' class='k-textbox' id='item{$i}'></td>";
       echo "<td><input type='text' data-row='{$i}' data-type='price' name='price{$i}' class='k-textbox' id='price{$i}'></td>";
       echo "<td><input type='text' data-row='{$i}' data-type='total' name='total{$i}' class='k-textbox' id='total{$i}'></td>";
    echo "<tr>";
}

then lets work on the jquery without any need for redundant loops

<script type="text/javascript">
   $(function () {
          $(".k-textbox").input(function (evt) {
               var that = $(this);
               if (that.attr('data-type') != 'price' && that.attr('data-type') != 'qty) return;
               var row = that.attr('data-row');
               var qty = parseInt($('#qty'+row).val());
               var price = parseFloat($('#price'+row).val());
               $('#total'+row).val((qty * price ? qty * price : 0).toFixed(2));
          });
    });
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.