Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy JavaScript Array (Prom Vote Script) problem :/

    Hello,
    I am new here so I am sorry if I made a mistake in using this forum. I am working in a Prom Vote Script for a School in my area and I am having an misstake in my Script. I am working now for days and I can't find the problem please help me I need to be finished in 2 days :/ - Here's the PHP Code which generates the JavaScript according to the Database informations:
    Code:
    <!DOCTYPE html>
    <html lang="en">
            <head>
                    <meta charset="utf-8" />
                    <title></title>
            </head>
            <body>
                    <?php
    $con = mysql_connect("localhost","xxx","xxxx");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    mysql_select_db(bgschwechat) or die( "Unable to select database");
    $abfrage = "SELECT * FROM teilnehmer";
    $ergebnis = mysql_query($abfrage);
    $abfrage2 = "SELECT * FROM teilnehmer";
    $ergebnis2 = mysql_query($abfrage2);
    echo '<script>';
    echo 'var stimmzahl = new Array();';
    echo 'var name = new Array();';
    echo '</script>';
    while($row = mysql_fetch_object($ergebnis))
            {
            echo '<script>';
            echo 'stimmzahl[';
            echo $row->id;
            echo '] = 0; ';
            echo 'name[';
            echo $row->id;
            echo '] = ';
            echo $row->name;
            echo '; ';
            echo 'function ';
            echo $row->id;
            echo 'plus() {';
            echo 'stimmzahl[';
            echo $row->id;
            echo ']++; ';
            echo 'document.getElementById(\'';
            echo $row->id;
            echo 'result\').innerHTML = console.log(stimmzahl[';
            echo $row->id;
            echo ']); ';
            echo '}';
            echo 'function ';
            echo $row->id;
            echo 'minus() {';
            echo 'stimmzahl[';
            echo $row->id;
            echo ']--; ';
            echo 'document.getElementById(\'';
            echo $row->id;
            echo 'result\').innerHTML = stimmzahl[';
            echo $row->id;
            echo ']; ';
            echo '}';
            echo '</script>';
            }
      
            echo '<table class="teilnehmer-liste">';
      
    while($row = mysql_fetch_object($ergebnis2))
            {
            echo '<tr>';
            echo '<td>';
            echo $row->name;
            echo '</td>';
            echo '<td>';
            echo '<button id="';
            echo $row->id;
            echo 'plus" onClick= "';
            echo $row->id;
            echo 'plus()">+</button>';
            echo '</td>';
            echo '<td>';
            echo '<button id="';
            echo $row->id;
            echo 'minus" onClick="';
            echo $row->id;
            echo 'minus()">-</button>';
            echo '</td>';
            echo '<td>';
            echo '<div id="';
            echo $row->id;
            echo 'result">/</div>';
            echo '</td>';
            echo '</tr>';
    }
    mysql_free_result($ergebnis);
    ?>
    </table>
            </body>
            </html>
    After executing the PHP Code I become this result in HTML - Everything seems correctly, except that the buttons don't work

    Code:
    <!DOCTYPE html>
    <html lang="en">
            <head>
                    <meta charset="utf-8" />
                    <title></title>
            </head>
            <body>
                    <script>var stimmzahl = new Array();var name = new Array();</script><script>stimmzahl[1] = 0; name[1] = Test Person 1; function 1plus() {stimmzahl[1]++; document.getElementById('1result').innerHTML = console.log(stimmzahl[1]); }function 1minus() {stimmzahl[1]--; document.getElementById('1result').innerHTML = stimmzahl[1]; }</script><script>stimmzahl[2] = 0; name[2] = Test Person 2; function 2plus() {stimmzahl[2]++; document.getElementById('2result').innerHTML = console.log(stimmzahl[2]); }function 2minus() {stimmzahl[2]--; document.getElementById('2result').innerHTML = stimmzahl[2]; }</script><table class="teilnehmer-liste"><tr><td>Test Person 1</td><td><button id="1plus" onClick= "1plus()">+</button></td><td><button id="1minus" onClick="1minus()">-</button></td><td><div id="1result">/</div></td></tr><tr><td>Test Person 2</td><td><button id="2plus" onClick= "2plus()">+</button></td><td><button id="2minus" onClick="2minus()">-</button></td><td><div id="2result">/</div></td></tr></table>
            </body>
            </html>
    It would be really nice if someone could give me a correct version of this code

    - Thanks in advance, ASavic

  • #2
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    First of all: Why do you wrap every single command into its own script tag? Just put one around all of them. Secondly, give your script tag a type (<script type"=text/javascript">).

    Now to the (syntactical, "big") mistakes. Strings need to be marked as such.

    Code:
    name[1] = Test Person 1;
    is wrong and should be

    Code:
    name[1] = "Test Person 1";
    By the way: Array indexing starts at 0.

    Secondly, functions cannot start with numbers ("function 1plus()" is illegal).

    Thirdly: Since when does Germany have proms?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •