0

after getting data from sql I tried to click ok the button to add 1 number (sum sql data number + 1) but the Java is still working on all the inputs for all the results that I an getting

<input  type="text"   id="<?php echo $row['number'];?>"  >

 <button value="1" id="<?php echo $row["id"];?>">1</button>     
<script>

var theTotal = 0;
$('#<?php echo $row["id"];?>').click(function(){
   theTotal = Number(theTotal) + Number($(this).val());
    $('#<?php echo $row["number"];?>').val(theTotal);        
});

$('#<?php echo $row["number"];?>').val(theTotal);
</script>
5
  • 1
    What is the resulting client-side code that this creates? Is it what you expect it to create? Commented Nov 6, 2019 at 23:45
  • 2
    All your buttons are using the same theTotal variable. Is that what you want? Commented Nov 6, 2019 at 23:45
  • You shouldn't redeclare the same global variable during the loop. It should be declared once before the loop. Commented Nov 6, 2019 at 23:52
  • for example, on my data base i have 2 records, and when i use php to select data from sql the data will show in text-box, then i want to click on the button to increase the number by one, what is happening if i click the button all text box for the 2 results will add 2 Commented Nov 6, 2019 at 23:53
  • Why do you need the variable theTotal? Just use $('#<?php echo $row["number"];?>').val(Number($(this).val())+1) Commented Nov 6, 2019 at 23:57

1 Answer 1

1

You don't need the theTotal variable, just get the value from the input field, add 1 to it, and put that back.

$('#<?php echo $row["id"];?>').click(function(){
    var val = Number($('#<?php echo $row["number"];?>'));
    $('#<?php echo $row["number"];?>').val(val + 1);        
});
Sign up to request clarification or add additional context in comments.

1 Comment

I already knew out that this is part of a loop processing the results of a database query. The $row variable makes this obvious.

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.