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