Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript help

Status
Not open for further replies.

vsantoro

Technical User
Joined
Sep 2, 2010
Messages
8
Location
US
can someone help.
its a little quiz script. it loads first question but wont load second question after you click.

Code:
<?php
include("config.php");
$sql_q = "SELECT * FROM questions"; $res_q = mysql_query($sql_q); $nr_questions = mysql_num_rows($res_q);
?>
<html>
<head>
<title><?=$TITLE?></title>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
var points = 0, nr_questions = <?=$nr_questions?>;
var answers = new Array();
<?php
$sqlr = "SELECT * FROM results ORDER BY points ASC"; $resr = mysql_query($sqlr); $i = 0;
while ($rowr = mysql_fetch_array($resr)) {
echo "answers[$i] = new Array(2);";
echo "answers[$i][0] = '".$rowr['points']."';";
echo "answers[$i][1] = '".$rowr['result']."';\n";
$i++;
}
?>
function add_points(nr,nrpoints) {
points = points + nrpoints;

document.getElementById("question"+nr).style.display="none";
try {
document.getElementById("question"+(nr+1)).style.display="inline";
} catch(err) {}
if (nr == nr_questions) {
document.location="result.php?result=" + points;
/*var i = 0;
while (points > answers[i][0])
i++;
alert("Total points: " + points + "\n" + answers[i][1]);*/
}
}
</script>
</head>
<body>
<table width="100%" border="0">
</table>
<table width="100%" border="0">
<tr>
</tr>
<tr>
<td><div align="center"><?php
$i = 1;
while ($rowq = mysql_fetch_array($res_q)) {
echo "<div id='question".$i."'";
if ($i != 1) echo ' style="display:none;" ';
echo "><h1>".$rowq['question']."</h1>";
$sql_a = "SELECT * FROM answers WHERE question=".$rowq['id']; $res_a = mysql_query($sql_a);
while ($rowa = mysql_fetch_array($res_a)) {
echo "<b><h2><a href='javascript:void(0);' onclick='add_points(".$i.",".$rowa['points'].");'>".$rowa['answer']."</a><br /></h2></b?";
}
echo '</div>'; $i++;
}
?>
</div>
</table>
</body>
</html>
 
Hi

Please post the generated document as seen by the browser. ( I mean what is displayed by the browser's View source command. )


Feherke.
 
sure here you go

Code:
<html>
<head>
<title>test?</title>
<SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
	var points = 0, nr_questions = 4;
	var answers = new Array();
	answers[0] = new Array(2);answers[0][0] = '10';answers[0][1] = 'test ! Contratulations';
answers[1] = new Array(2);answers[1][0] = '500';answers[1][1] = 'You are a Disgusting Pervert !!';
	function add_points(nr,nrpoints) {
		points = points + nrpoints;

		document.getElementById("question"+nr).style.display="none";
		try {
		document.getElementById("question"+(nr+1)).style.display="inline";
		} catch(err) {}
		if (nr == nr_questions) {
			document.location="result.php?result=" + points;
			/*var i = 0;
			while (points > answers[i][0])
				i++;
			alert("Total points: " + points + "\n" + answers[i][1]);*/
		}		
	}
</script>
</head>
<body>
<table width="100%" border="0">
  <tr>
    <td><div align="center"><img src="a.jpg" width="1008" height="264"></div></td>
  </tr>
  <tr>

    <td>&nbsp;</td>
  </tr>
</table>
<table width="100%" border="0">
  <tr>
    <td><div align="center"><img src="s2.jpg" width="800" height="100"></div></td>
  </tr>
  <tr>
    <td><div align="center"><img src="s.jpg" width="800" height="5"></div></td>
  </tr>

  <tr>
    <td><div align="center"><div id='question1'><h1>test</h1><b><h2><a href='javascript:void(0);' onclick='add_points(1,10);'>1</a><br /></h2></b?<b><h2><a href='javascript:void(0);' onclick='add_points(1,0);'>2</a><br /></h2></b?</div><div id='question2' style="display:none;" ><h1>test1</h1><b><h2><a href='javascript:void(0);' onclick='add_points(2,8);'>1</a><br /></h2></b?<b><h2><a href='javascript:void(0);' onclick='add_points(2,0);'>2</a><br /></h2></b?</div><div id='question3' style="display:none;" ><h1>test2</h1><b><h2><a href='javascript:void(0);' onclick='add_points(3,10);'>1</a><br /></h2></b?<b><h2><a href='javascript:void(0);' onclick='add_points(3,0);'>2</a><br /></h2></b?</div><div id='question4' style="display:none;" ><h1>test3</h1><b><h2><a href='javascript:void(0);' onclick='add_points(4,0);'>1</a><br /></h2></b?<b><h2><a href='javascript:void(0);' onclick='add_points(4,15);'>2</a><br /></h2></b?</div>    </div></td>

  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>

  </tr>
  <tr>
    <td><div align="center"></div></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>

  </tr>
  <tr>
    <td>test</td>
  </tr>
</table>
</body>
</html>
 
Hi

First you should learn to write syntactically and semantically valid HTML.

Your JavaScript works correctly. But [tt]div[/tt] question2 is child of [tt]div[/tt] question1, so even if you set the later's [tt]display[/tt] to 'inline', it will not appear as its parent node has [tt]display[/tt] 'none'.

All this due to a typo in your PHP script ( marked with red below ) :
Code:
[b]while[/b] [teal]([/teal][navy]$rowa[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]mysql_fetch_array[/color][teal]([/teal][navy]$res_a[/navy][teal]))[/teal] [teal]{[/teal]
[b]echo[/b] [green][i]"<b><h2><a href='javascript:void(0);' onclick='add_points("[/i][/green][teal].[/teal][navy]$i[/navy][teal].[/teal][green][i]","[/i][/green][teal].[/teal][navy]$rowa[/navy][teal][[/teal][green][i]'points'[/i][/green][teal]].[/teal][green][i]");'>"[/i][/green][teal].[/teal][navy]$rowa[/navy][teal][[/teal][green][i]'answer'[/i][/green][teal]].[/teal][green][i]"</a><br /></h2></b[COLOR=red yellow]?[/color]"[/i][/green][teal];[/teal]
[teal]}[/teal]
Some additional advices :
[ul]
[li]Use a DOCTYPE, preferably a strict one, then check it with The W3C Markup Validation Service.[/li]
[li]Use [tt]table[/tt] tags to format tabular data, not for layout.[/li]
[li]Use heading tags to mark headings, not to set text appearance.[/li]
[li]Do not put block elements into inline elements.[/li]
[/ul]


Feherke.
 
thank you, that was it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top