954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

2 drop down lists and a table on same page (PHP)

Hi
I got 1 static drop down list, 1 dynamic drop down list(shows content of a mysql table) and content of another mysql table on the same webpage. They are all placed on 3 different forms.
The selected option in the first list triggers the 2nd list on the page. However, this works fine.
My problem is that i cannot get the data from the 2nd table displayed once the user has selected a value from the 2nd drop down list.
I know I could try and display the last table on a different page but it all has to be on the same page.
Here's some code:

//first form
<form name="reporttype" method="post" action="report.php?act=submit">
        <label>
         <span class="style16">View report by </span>
         <select name="parameters" id="parameters" class="style16" >
              <option>Client </option>
              <option>Supplier</option>
              <option>URL</option>
         </select>
        </label>
            <p>
              <label>
              <input name="submit" type="submit" id="submit" value="Submit" >
              </label>
        </p>
	  </form>

//2nd form
 <form action="report.php?act=view" method="post" name="textform" class="style16">
       <?php    
        require_once("functions.php");
   	    $connection = db_connect();
	    $suppliers=mysql_query("select * from supplier order by company");
	    $clients=mysql_query("select * from client order by company");
	    $n=mysql_numrows($suppliers);
	    $m=mysql_numrows($clients);
	    //$action = $_GET['act'];  
	    if ($_POST['submit'])
	    { 
		 $option = $_POST['parameters']; 
		 switch ($option) 
		  {
            //default: header("location:report.php"); break;
            case "Client":
			  $label= "Client Company: ";
			  echo $label;
			  echo "<select name=\"parameter\"  id=\"parameter\">";
			    $i=0; 
				while ($i<$m)
				 { $row = mysql_fetch_array($clients);
				   echo "<option value=\"".$row['company']."\">".$row['company']."\n  ";
				   $i++; 
				 }
			  echo " </select>";
			  echo "";
			  echo"<input name=\"view\" type=\"submit\" id=\"view\" value=\"View Reports\">";
			  echo"<input type=\"hidden\" name=\"label\" value=\"".$label."\">";
			  break;
            case "Supplier":
			  $label="Supplier Company: ";
			  echo $label;
			  echo "<select name=\"parameter\"  id=\"parameter\">";
			    $i=0; 
				while ($i<$n)
				 { $row = mysql_fetch_array($suppliers);
				   echo "<option value=\"".$row['company']."\">".$row['company']."\n  ";
				   $i++; 
				 }
			  echo " </select>";
			  echo "";
			  echo"<input name=\"view\" type=\"submit\" id=\"view\" value=\"View Reports\">";
			  echo"<input type=\"hidden\" name=\"label\" value=\"".$label."\">";
			  break;
            case "URL": 
			  $label="URL: ";
			  echo $label;
			  echo "<select name=\"parameter\"  id=\"parameter\">";
			    $i=0; 
				while ($i<$m)
				 { $row = mysql_fetch_array($clients);
				   echo "<option value=\"".$row['url']."\">".$row['url']."\n  ";
				   $i++; 
				 }
			  echo " </select>";
			  
			  echo "";
			  echo"<input name=\"view\" type=\"submit\" id=\"view\" value=\"View Reports\">";
			  echo"<input type=\"hidden\" name=\"label\" value=\"".$label."\">";
			  break;
		
         }	 
		}	
?></p>
      </form>

//3rd form
<?php
	 $action = $_GET['act'];
     if ($action == viewrep)
	 //  if (($_POST['parameter'])&&($_POST['view']))
	   { 
		 $parameter= $_POST['parameter'];
		 echo $parameter;
		 $label=$_POST['label'];
		 switch ($label) 
		 {
          
          case "Client Company: ": 
			  $query=mysql_query("select * from report where companyname='$parameter' and reporttype='client' order by id desc");
	           $n=mysql_numrows($query);
			   echo "<table>"; 
               $i=0; 
		       while ($i<$n)
			    { $row = mysql_fetch_array($query);
			      $r=$row['companyname'];
			      echo"<tr>"; 
                  echo"<td>"; 
                  echo"<a href='reportclient.php\?name=$r'>$r</a\>";
                  echo"</td>"; 
                  echo"</tr>";
			    } 
               echo"</table>";
			   echo"<input name=\"viewrep\" type=\"submit\" id=\"viewrep\" value=\"View Report\">";
    }

		 
} 
 ob_end_flush();
?>
        </span>
            </form>
michelleradu
Junior Poster in Training
55 posts since Dec 2009
Reputation Points: 10
Solved Threads: 5
 

That might be because the value of act in line 19 is not equal to the conditional value in line 89. Change line 89 to

if ($action == "view")


Does that help anything?

CJesusSaves
Light Poster
49 posts since Jan 2010
Reputation Points: 16
Solved Threads: 5
 

I've changed line 89 to
if ($action == view)
but all i get now is a HTTP 500 Internal Server Error. :(

michelleradu
Junior Poster in Training
55 posts since Dec 2009
Reputation Points: 10
Solved Threads: 5
 

if ($action == "view") not
if ($action == view)

Note quotations...

CJesusSaves
Light Poster
49 posts since Jan 2010
Reputation Points: 16
Solved Threads: 5
 

if ($action == "view") not if ($action == view)

Note quotations...


It still doesn't work... with or without quotations. But thanks anyway.

michelleradu
Junior Poster in Training
55 posts since Dec 2009
Reputation Points: 10
Solved Threads: 5
 

It should work, but anyhow, right after line 83, before , add another hidden input field

On line 88, instead of $_GET, use:
$action = $_POST['act'];

By the way, your third form has no header form tag and vars. See lines 86-87.

CJesusSaves
Light Poster
49 posts since Jan 2010
Reputation Points: 16
Solved Threads: 5
 

Another thing I just noticed, you've got three different drop down menus called parameter, if I was PHP I'd be really confused whenever I reach line 92, and most certainly would cough an error. If the above suggestion does not work, comment out line 92 with:

//$parameter= $_POST['parameter'];

Then add, and edit accordingly:

$parameter = "A value from any drop down goes here";
CJesusSaves
Light Poster
49 posts since Jan 2010
Reputation Points: 16
Solved Threads: 5
 

if ($action == "view") not if ($action == view)

Note quotations...

I followed your advices and it still doesnt work, but I think I made some progress.
In the code I've posted earlier, i missed (copying) the form header line:



The last form picks up the value of $action(=view) correctly, but it doesn't display the table, so now the problem might be in the switch structure.

michelleradu
Junior Poster in Training
55 posts since Dec 2009
Reputation Points: 10
Solved Threads: 5
 

I've changed the name of the 2nd drop down to dd2 so now I have:

$dd2value= $_POST['dd2'];
echo $dd2value;


At the end, the whole thing is not working...

michelleradu
Junior Poster in Training
55 posts since Dec 2009
Reputation Points: 10
Solved Threads: 5
 

Well that's good, do yourself a favour, right after line 94, echo $label;
and right after line 105, add: echo $r; You might also want to add some dot operators on line 108. I'm not sure how other folks like to program, but I like checking variables to ensure that they contain values that I expect.

By the way, on line 120 you've got a close span but no open span.

CJesusSaves
Light Poster
49 posts since Jan 2010
Reputation Points: 16
Solved Threads: 5
 

First form's tag doesn't have values. So, it will never enter the switch cases of the 2nd form. And as CJesusSaves said, It's better to keep php and html separate as it is easy to manage [and you don't have to worry about escaping quotes ['] and ["]].

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 

For line 108:

echo "<a href='reportclient.php\?name=".$r."'>".$r."</a\>";
CJesusSaves
Light Poster
49 posts since Jan 2010
Reputation Points: 16
Solved Threads: 5
 

Well that's good, do yourself a favour, right after line 94, echo $label; and right after line 105, add: echo $r; You might also want to add some dot operators on line 108. I'm not sure how other folks like to program, but I like checking variables to ensure that they contain values that I expect.

By the way, on line 120 you've got a close span but no open span.

$label and $r are displayed correctly.
Don't worry about missing html code, I haven't copied the whole source in here, just the relevant parts.

michelleradu
Junior Poster in Training
55 posts since Dec 2009
Reputation Points: 10
Solved Threads: 5
 

your form 2 is like this

<form action="report.php?act=view" method="post" name="textform" class="style16">


so you should changed this code

if ($action == viewrep)

to

if ($action == 'view')
vaultdweller123
Posting Pro
554 posts since Sep 2009
Reputation Points: 42
Solved Threads: 75
 

your form 2 is like this

<form action="report.php?act=view" method="post" name="textform" class="style16">

so you should changed this code

if ($action == viewrep)

to

if ($action == 'view')


I 've already done that

if ($action == 'view')
michelleradu
Junior Poster in Training
55 posts since Dec 2009
Reputation Points: 10
Solved Threads: 5
 

I suggest you to go step by step. First make first form work, then 2nd and so on. It's easy to debug that way.

nav33n
Purple hazed!
Moderator
4,465 posts since Nov 2007
Reputation Points: 524
Solved Threads: 356
 
I suggest you to go step by step. First make first form work, then 2nd and so on. It's easy to debug that way.


I shall do that, thank you.
I'll keep you updated.

michelleradu
Junior Poster in Training
55 posts since Dec 2009
Reputation Points: 10
Solved Threads: 5
 

yeah i patiently trace your code and the only thing that i suspect the problem would be in your query

$query=mysql_query("select * from report where companyname='$parameter' and reporttype='client' order by id desc");

coz if you form2 is working right and contain values , your query is right and on how you fetch you query is right.. then theres should be no problem traversing the recorset.

and also you can fetch your query like this

$query=mysql_query("select * from report where companyname='$parameter' and reporttype='client' order by id desc");
				while($row=mysql_fetch_array($query)){
				echo "<tr>
                 <td>
                  <a href='reportclient.php?name=".$row['companyname']."'>".$row['companyname']."</a\>
                  </td>
                  </tr>";
				}
				echo"</table>
				<input name=\"viewrep\" type=\"submit\" id=\"viewrep\" value=\"View Report\">";
vaultdweller123
Posting Pro
554 posts since Sep 2009
Reputation Points: 42
Solved Threads: 75
 

yeah i patiently trace your code and the only thing that i suspect the problem would be in your query

$query=mysql_query("select * from report where companyname='$parameter' and reporttype='client' order by id desc");

coz if you form2 is working right and contain values , your query is right and on how you fetch you query is right.. then theres should be no problem traversing the recorset.

and also you can fetch your query like this

$query=mysql_query("select * from report where companyname='$parameter' and reporttype='client' order by id desc");
				while($row=mysql_fetch_array($query)){
				echo "<tr>
                 <td>
                  <a href='reportclient.php?name=".$row['companyname']."'>".$row['companyname']."</a\>
                  </td>
                  </tr>";
				}
				echo"</table>
				<input name=\"viewrep\" type=\"submit\" id=\"viewrep\" value=\"View Report\">";


You are right. The problem was in the sql query.
I fixed it and now everything works fine.
Thank you all for your help. :)

michelleradu
Junior Poster in Training
55 posts since Dec 2009
Reputation Points: 10
Solved Threads: 5
 

glad to help.

vaultdweller123
Posting Pro
554 posts since Sep 2009
Reputation Points: 42
Solved Threads: 75
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You