Calculate percentage in dynamic field

Asked

Viewed 575 times

-1

I have inputs like text where a javascript function takes these input values and makes a percentage account, works correctly, the problem is that depending on the input data these fields become dynamic, generating several inputs with the same ID, then the javascript function cannot reference and does not do the calculation. Does anyone have any idea how I could solve this or change the method for the calculation?

PS: the system used to calculate the data received in php, but the user needs to see the result of the account in real time when he inserts the value of the percentage.

  • 1

    Put the code as far as you can so we can help you.

  • Elements on the same page that share the same ID is invalid, incorrect and will never work. The solution to your problem is to stop working with id and start working with class.

1 answer

0

"will be 3 inputs with the same ID" I don’t know how your code is, but just to show you that you can recover everything from the other side of php - even with elements of the same id.

I suggest you generate the id dynamically (I don’t know if that’s your question) but here’s a code that shows you can pass the information and recover with POST in php.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <form action="test.php" method="post">
        <input id="as"  type="text" name="valor[]" value="1"><br>
        <input id="as"  type="text" name="valor[]" value="2"><br>
        <input id="as"  type="text" name="valor[]" value="3"><br>
        <input id="as"  type="submit" value="Submit">

        </form>

        <?php

        $arr=$_POST["valor"];
        echo '<pre>';
        print_r($arr);
        echo '</pre>';
        ?>
    </body>
</html>

Note what happens in the POST array: inserir a descrição da imagem aqui

Test this code to retrieve values using classes and not Ids

//HTML
<form action="test.php" method="post">
  <input id="as" class="tosend" type="text" name="valor[]" value="1"><br>
  <input id="as" class="tosend" type="text" name="valor[]" value="2"><br>
  <input id="as" class="tosend" type="text" name="valor[]" value="3"><br>
  <input id="btntosend" class="btntosend" type="submit" value="Submit">
</form>
//Js
$('#btntosend').on( 'click', function(e) { 
    e.preventDefault();
   $('.tosend').each(function() {//Passar por cada radio da classe rdShipMo
      alert($(this).val());
   });   
});

See the fiddle. You don’t even need Id.

  • Thank you for answering @zwitterion, this dynamic array is already being done in the code, this is exactly the problem with javascript, using your example above, suppose that this input has an id and javascript uses it as a reference to fetch a value, ai quando ele for dinâmico (3 inputs as in the example) js function will not be able to find the reference (because it has 3 inputs with the same id), understand?

  • Yes I understand. I was showing that even with memo id this technique of using array as name the POST or GET can recover the values. So you have two possibilities: 1- create Ids dynamically with javascript or 2-change your.

  • I will edit my reply to show you how you can do different using JS

Browser other questions tagged

You are not signed in. Login or sign up in order to post.