0

I am quite new to jquery, and i couldn't use .each function in a php foreach function. I am trying to do a quantity, unit price, total price table. I don't know how many table rows does user need, therefore used foreach function. Here is an example what i am trying to do:

my deneme2.php file :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width= <script type="text/javascript">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>deneme5.js</script>
</head>
<body>
<?php
$arr=[1,2];
foreach ($arr as $key=>$val){ ?>
<div>
    <input id="tst1"; type="text"; >
    <input id="tst2"; type="text"; >
    <input id="tst3"; type="text"; > <br><br>
  </div>
<?php } ?>
<input  id="asd"; type="submit"; value="hesapla">
</body>
</html>

And my deneme5.js file:

$(function(){
    $("#asd").click(function(){

        $("div").each(function(index){

            var a=$("#tst1").val();
            var b=$("#tst2").val();
            var c= parseInt(a)*parseInt(b);
            $("#tst3").val(c);

              });

    });


  });

Can you help me? what am i doing wrong?

3
  • 1
    jQuery runs in browser, PHP on server. Commented Mar 5, 2020 at 13:21
  • 1
    What you did here : <script>deneme5.js</script> Commented Mar 5, 2020 at 13:30
  • ID's Must Be Unique, specifically because it will cause problems in JavaScript and CSS when you try to interact with those elements. Commented Mar 5, 2020 at 13:59

1 Answer 1

1

You have many syntax errors in your code. I have revised it a bit. Please compare it with yours and see where you have your mistakes.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
    <!-- here i have added your javascript code directly. You can include it like that: <script src="deneme5.js"></script>-->
    <script>
    $(function(){
      $("#asd").click(function()  {

        $(".row").each(function(index, element){
          var a = $(element).find(".tst1").val();
          var b = $(element).find(".tst2").val();
          var c = parseInt(a) * parseInt(b);
          $(element).find(".tst3").val(c);
        });

      });
    });
    </script>

</head>
<body>

<?php
$arr=[1,2];
foreach ($arr as $key=>$val){ ?>
<!-- in the following i have switched your ids into classes because ids have to be unique. Furthermore html attributes do not have to be separated by ';' -->
<div class="row">
    <input class="tst1" type="text">
    <input class="tst2" type="text">
    <input class="tst3" type="text"><br><br>
  </div>
<?php } ?>
<input id="asd" type="submit" value="hesapla">

</body>
</html>

One of the things done here was to convert the use of ID's to classes. ID's cannot be duplicated on a page because it will cause problems with JavaScript and CSS.

Sign up to request clarification or add additional context in comments.

3 Comments

Remember - A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
@JayBlanchard - Therefore i have commented my changes in the code.
I understand @RobinGillitzer, but it has been found that explanations outside of code comments generally work better for folks and makes the answer more searchable.

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.