...

View Full Version : Small Error, Can't find it



mike36
08-26-2007, 10:17 AM
Hello,
I have a small error I can't find. When I try to pick a different question in my FAQ it keeps opening the first answer. Please visit the problem, here (http://mikes-pc-repair.com/faq/). First click on a section. (One of the first three) Then try to pick the second question. The answer for question one only opens. Please help, here is the code.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>

<head>

<?php

$print = isset($_GET['print']) ? $_GET['print'] : false;

$category_id = isset($_POST['category_id']) ? $_POST['category_id'] : $_GET['category_id'];

$cat_name = isset($_POST['cat_name']) ? $_POST['cat_name'] : $_GET['cat_name'];

$answer = isset($_POST['answer']) ? $_POST['answer'] : 0;





include "faq_config.php";



?>



<title><?php echo "$cat_name" ?></title>



<META HTTP-EQUIV="expires" CONTENT="0">

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">



<link rel="stylesheet" type="text/css" href="style.css">



<style>

a {text-decoration: none}

</style>



<script language="JavaScript">

<!--



function submit_faq(){

var obj = document.getElementById("quest");

obj.submit();

}



var howManySeconds = 1



//--This is the function that pauses, then calls your custom function.

function pause() {

myTimer=setTimeout("whatToDo()",howManySeconds*1000)

}



//-- This is the function that's executed after the delay time is up.

function whatToDo() {

window.print()

}

//-->

</script>



</head>



<body <?php if ($print) { print "onload=\"pause()\""; } ?>>

<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">

<tr>

<td class="poptop" height="15"><img src="images/spacer.gif" width="3" height="3"></td>

</tr>

<tr>

<td class="hrz_line"><img src="images/spacer.gif" width="1" height="1"></td>

</tr>

<tr>

<td style="background: #ffffff; padding: 5px" valign="middle"><h1 style="margin-top: 0; margin-bottom: 0"><?php echo "$cat_name" ?></h1></td>

</tr>

<tr>

<td class="hrz_line"><img src="images/spacer.gif" width="1" height="1"></td>

</tr>

<tr>

<td valign="top" style="padding: 1em" class="maincontent" height="400">



<p><?php echo $faq_instruct; ?></p>

<p><a href="<?php echo "$PHP_SELF?print=true&cat_name=$cat_name&category_id=$category_id" ?>"><?php echo "$s_print_faq" ?></a></p>





<table width="100%" border="0" cellpadding="8" cellspacing="0">



<?php



$result = mysql_query("SELECT * FROM faqs WHERE category_id = '$category_id'") or die(mysql_error());





while ($row = mysql_fetch_array($result)) {



if (($answer == "$row[id]") || ($print == "true")) {



print "<tr>

<td class=\"QA\" valign=\"top\">Q </td>";



print "<form name=\"quest\" id=\"quest\" action=\"$PHP_SELF\" method=\"post\">\n";

print "<input type=\"hidden\" name=\"category_id\" value=\"$category_id\"></p>\n";

print "<input type=\"hidden\" name=\"cat_name\" value=\"$cat_name\">\n";

print "<input type=\"hidden\" name=\"answer\" value=\"0\">\n";

print "</form>\n<td><pre class=\"question\"><a href=\"javascript:submit_faq();\">$row[question]</a></pre>";

print "</td></tr>\n";



print "<tr>

<td class=\"QA\" valign=\"top\">A</td>

<td><pre class=\"answer\">$row[answer]</pre>

<br>

<a href=\"$PHP_SELF?answer=0&cat_name=$cat_name&category_id=$category_id#$answer\">" . $s_close . "</a><br></td>

</tr>\n";

}





else {



print "<tr>

<td class=\"QA\" valign=\"top\">Q <a name=\"$row[id]\"></td>

<td>";

print "<form name=\"quest\" id=\"quest\" action=\"$PHP_SELF\" method=\"post\">\n";

print "<input type=\"hidden\" name=\"category_id\" value=\"$category_id#$row[id]\"></p>\n";

print "<input type=\"hidden\" name=\"cat_name\" value=\"$cat_name\">\n";

print "<input type=\"hidden\" name=\"answer\" value=\"$row[id]\">\n";

print "</form>\n

<pre class=\"question\"><a href=\"javascript:submit_faq();\">$row[question]</a></pre></td>

</tr>\n";

}





}





mysql_close($link);



?>



</table>



</td>

</tr>

<tr>

<td class="hrz_line"><img src="images/spacer.gif" width="1" height="1"></td>

</tr>

<tr>

<td height="20" class="popbot"><a href="javascript:window.close()"><?php echo "$s_close" ?></a></td>

</tr>

</table>



</body>

</html>

mcjwb
08-26-2007, 01:59 PM
If you give the same id to all those forms how do you expect this bit of code to work?

function submit_faq(){
var obj = document.getElementById("quest");
obj.submit();
}
It will simply return the first form it finds with id=quest, hence only the first answer will ever get displayed.
Every element on your page must have a unique id (if it is assigned one).

This isn't really a javascript question because you don't need to use javascript, simply create your links like this:


<a href="faq.php?category_id=2#3#5&cat_name=Computer%20Repairs&answer=5">What type of computers do you repair?</a>
for example, and get rid of all those forms.
NB you should use the php function urlencode() (http://uk.php.net/urlencode) when putting php variables in the query string of the url.

mike36
08-26-2007, 02:17 PM
It didn't seem to work when I replaced my links. It didn't open anything.

mcjwb
08-26-2007, 02:23 PM
Oh right, you'll need to change this line:

$answer = isset($_POST['answer']) ? $_POST['answer'] : 0;
to this:

$answer = isset($_POST['answer']) ? $_POST['answer'] : (isset($_GET['answer']) ? $_GET['answer'] : 0);

mike36
08-26-2007, 09:28 PM
The same problem occurred as above. The answers aren't opening at all, not even the first one. (Itches Head)

rwedge
08-26-2007, 10:21 PM
mcjwb gave you what you needed.

Using this (http://mikes-pc-repair.com/faq/) page.

function submit_faq(){
var obj = document.getElementById("quest");
obj.submit();
}
will always submit the first form with the id of 'quest'

Give each form a unique id and pass it to the submit_faq function
function submit_faq(which){
document.getElementById(which).submit();
}
<a href="javascript:submit_faq('quest');">Qustion 1</a>
<a href="javascript:submit_faq('quest1');">Qustion 2</a>

mcjwb
08-26-2007, 11:51 PM
I think it all should work, perhaps the path for the link is wrong, in the new link I told you to use try replacing "faq.php" with $PHP_SELF.

If that doesn't help, post what you've done so I'll be able to see exactly where the problem lies.

mike36
08-27-2007, 12:37 AM
Is this correct? I'm very sorry but I haven't taken javascript classes yet. I plan to this fall.


function submit_faq(){

var obj = document.getElementById("quest");

obj.submit();

}

function submit_faq(which){
document.getElementById(which).submit();
}
<a href="javascript:submit_faq('quest');">If I'm not sure what the problem is will you diagnose it for me?</a>
<a href="javascript:submit_faq('quest1');">What type of computers do you repair?</a>

rwedge
08-27-2007, 02:55 AM
You can remove the top function in red as the one below it replaces it:
function submit_faq(){

var obj = document.getElementById("quest");

obj.submit();

}
function submit_faq(which){
document.getElementById(which).submit();
}
Make sure you give each form the proper id - quest, quest1, quest 2, etc..
print "<form name=\"quest\" ...
...
print "<form name=\"quest1\" ..

mike36
08-27-2007, 02:59 AM
Thanks, it seems to be working now! You guys are life savers. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum