Jump to content

JavaScript and PHP what am I doing wrong? Please help :D


ASavic

Recommended Posts

Hello,I'm new here and to be honest it's the first time I am writing into a Forum. Until now I was only reading through various posts which considerd the same problem as I had. But now I really have no idea what to search and where my problem is. Down here is the PHP Script I have written for a Prom Vote in a nearby school. It would be really great if someone could find my mistake and correct me.

<!DOCTYPE html><html lang="en">	<head>		<meta charset="utf-8" />		<title></title>	</head>	<body>		<?php$con = mysql_connect("localhost","xxx","xxxxxxxxx");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 = array();';echo 'var name = array();';echo '</script>';while($row = mysql_fetch_object($ergebnis))	{	echo '<script>';	echo 'stimmzahl[';	echo $row->id;	echo '] = 0; ';	echo 'name[';	echo $row->id;	echo '_name';	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 = 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= "function ';	echo $row->id;	echo 'plus()">+</button>';	echo '</td>';	echo '<td>';	echo '<button id="';	echo $row->id;	echo 'minus" onClick=" function ';	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>

The PHP Code works pretty fine but I am not happy with the result because the + and - Buttons don't work :/ Here's the result of the PHP Code in HTML :

<!DOCTYPE html><html lang="en">	<head>		<meta charset="utf-8" />		<title></title>	</head>	<body>		<script>var stimmzahl = array();var name = array();</script><script>stimmzahl[1] = 0; name[1_name] = Test Person 1; function 1plus() {stimmzahl[1]++; document.getElementById("1result").innerHTML = stimmzahl[1]; }function 1minus() {stimmzahl[1]--; document.getElementById("1result").innerHTML = stimmzahl[1]; }</script><script>stimmzahl[2] = 0; name[2_name] = Test Person 2; function 2plus() {stimmzahl[2]++; document.getElementById("2result").innerHTML = 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= "function 1plus()">+</button></td><td><button id="1minus" onClick=" function 1minus()">-</button></td><td><div id="1result">/</div></td></tr><tr><td>Test Person 2</td><td><button id="2plus" onClick= "function 2plus()">+</button></td><td><button id="2minus" onClick=" function 2minus()">-</button></td><td><div id="2result">/</div></td></tr></table>	</body>	</html>

It would be really great if someone could help me I need to find a solution as soon as possible because I only have rare time to finish the project. Thanks in advance. ASavic :D P.S. Here's the Website Link - http://www.cryze.com/bgschwechat/test.php - sometimes offline because it's in development. ^^

Edited by ASavic
Link to comment
Share on other sites

Thanks for fast help Deirdre's Dad I removed the _name so it is a integer and not a string so the "" aren't necessery anymore, but it's stil not working :/ does anyone have an idea???

Edited by ASavic
Link to comment
Share on other sites

Are you using an error console of any kind? Several errors turn up there very clearly. 1. array() is not a built-in function. The array constructor looks like this: new Array(); the "new" keyword and uppercase A are required. JS is case-sensitive. 2. Should this be in quotations marks: Test Person 2 ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...