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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Small Error, Can't find it

    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. 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.

    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>

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    If you give the same id to all those forms how do you expect this bit of code to work?
    Code:
    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:

    Code:
    <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() when putting php variables in the query string of the url.

  • #3
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It didn't seem to work when I replaced my links. It didn't open anything.

  • #4
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Oh right, you'll need to change this line:
    Code:
    $answer = isset($_POST['answer']) ? $_POST['answer'] : 0;
    to this:
    Code:
    $answer = isset($_POST['answer']) ? $_POST['answer'] : (isset($_GET['answer']) ? $_GET['answer'] : 0);

  • #5
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The same problem occurred as above. The answers aren't opening at all, not even the first one. (Itches Head)

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    mcjwb gave you what you needed.

    Using this page.
    Code:
      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
    Code:
    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>

  • #7
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    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.

  • #8
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is this correct? I'm very sorry but I haven't taken javascript classes yet. I plan to this fall.

    Code:
    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>
    Last edited by mike36; 08-27-2007 at 12:40 AM.

  • #9
    Regular Coder
    Join Date
    Feb 2005
    Posts
    679
    Thanks
    0
    Thanked 16 Times in 15 Posts
    You can remove the top function in red as the one below it replaces it:
    Code:
    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..
    Code:
    print "<form name=\"quest\" ...
     ...
    print "<form name=\"quest1\" ..

  • #10
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, it seems to be working now! You guys are life savers.


  •  

    Posting Permissions

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