-1

i have this jQuery function that multiplies the user's input on the value set and outputs the result on the same textbox. But my groupmate added a loop and made the textboxes hold an array of info. It suddenly stopped working. Is it the loop that prevents the function from executing?

<?php
    if(!isset($_POST['submit'])){
    $con=mysql_connect('localhost', 'root', '') or die ("Unable to connect!");
    mysql_select_db('SoftEng') or die ("Unable to select database");
    $query="SELECT * FROM login  WHERE AccountType='Student' ORDER by Lname ASC";
    $result=mysql_query($query) or die ("Unable to execute query" .mysql_error());
?>

<?php
    while($row=mysql_fetch_object($result)){
?>

<tr><?php echo "<input type=hidden name=stud[] value=$row->ID>"?>
    <td style="vertical-align:middle" class="text-justify"><i class="fa fa-fw fa-user"></i> <?php echo $row->Lname.", ".$row->Fname;?></td>
    <td style="vertical-align:middle">
            <input type="text"  name="quiz[]" id="quiz" size="8" class="text-center">
            <script>
                    $('#quiz').on('change', function (){
                            $(this).val($(this).val() * .10);
                    compute();
                    });
            </script>
    </td>

    <td style="vertical-align:middle">
            <input type="text" name="recitation[]" id="recitation" size="8" class="text-center">
            <script>
                    $('#recitation').on('change', function (){
                            $(this).val($(this).val() * .05);
                    compute();
                    });
            </script>
    </td>
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • Post your code please. – j08691 Oct 15 '14 at 16:55
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide. – Jay Blanchard Oct 15 '14 at 17:29

1 Answers1

2
  <input type="text"  name="quiz[]" id="quiz" size="8" class="text-center">
                                     ^^^^^^^^

DOM id's must be unique across the ENTIRE document. If you're adding multiple of these inputs, then you'll have duplicate IDs. Switch to using a class instead, e.g.

<input class="quiz" ...>

$('.quiz').on(.....);
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • You're also spitting out your jquery code inside the loop, so if you've got 10 database records being output, then you're doing the `.on()` stuff 10 times as well. – Marc B Oct 15 '14 at 17:08
  • So I'll get them out the loop? But the multiple insert would be affected. – Benedict John Payot Oct 15 '14 at 17:12