Member Avatar for Member #669958

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\"><br />";
			    $i=0; 
				while ($i<$m)
				 { $row = mysql_fetch_array($clients);
				   echo "<option value=\"".$row['company']."\">".$row['company']."\n  ";
				   $i++; 
				 }
			  echo " </select>";
			  echo "<br /><br />";
			  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\"><br />";
			    $i=0; 
				while ($i<$n)
				 { $row = mysql_fetch_array($suppliers);
				   echo "<option value=\"".$row['company']."\">".$row['company']."\n  ";
				   $i++; 
				 }
			  echo " </select>";
			  echo "<br /><br />";
			  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\"><br />";
			    $i=0; 
				while ($i<$m)
				 { $row = mysql_fetch_array($clients);
				   echo "<option value=\"".$row['url']."\">".$row['url']."\n  ";
				   $i++; 
				 }
			  echo " </select>";
			  
			  echo "<br /><br />";
			  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>

Dani AI

Generated

Quick, practical rundown and fixes (pulled together from the thread): the missing table output here usually comes from a mix of state/POST confusion, a bad SQL condition, or a PHP error that is hidden by the server. was right to flag missing option values, correctly warned about unquoted constants and passing state, and ultimately traced the live bug back to the SQL — all of these are common culprits. Below are concise, actionable steps to make this reliable and safe.

  • Keep form state consistent: either submit a single form that contains the first select, the populated second select, and the final submit; or if you use multiple forms, ensure each form includes the hidden state it needs (eg. a hidden "act" field) and that method/action are correct.
  • Use unique input names for different controls (report_by, item, view_action) — do not reuse the same name across forms. Reused names lead to overwritten/empty POST values.
  • Turn on error reporting while debugging and check server error logs (HTTP 500 almost always leaves a message there):
    error_reporting(E_ALL); ini_set('display_errors',1);
    Also dump $_POST/$_GET to confirm what the server actually received.
  • Whitelist input before using it in SQL. Map allowed report types to the exact column and reporttype you expect — never inject user input directly into SQL.

Example (safe pattern using PDO and a whitelist for column selection):

<?php
// $pdo is a configured PDO instance
$map = [
  'client' => ['col'=>'companyname','type'=>'client'],
  'supplier' => ['col'=>'companyname','type'=>'supplier'],
  'url' => ['col'=>'url','type'=>'client'],
];

if (!empty($_POST['report_by']) && !empty($_POST['item']) && isset($map[$_POST['report_by']])) {
  $col = $map[$_POST['report_by']]['col'];
  $type = $map[$_POST['report_by']]['type'];
  $stmt = $pdo->prepare("SELECT * FROM report WHERE $col = ? AND reporttype = ? ORDER BY id DESC");
  $stmt->execute([$_POST['item'], $type]);
  while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<tr><td><a href="reportclient.php?name='.htmlspecialchars($row[$col]).'">'.htmlspecialchars($row[$col]).'</a></td></tr>';
  }
}
?>

Extra notes: use prepared statements (PDO or mysqli), escape output with htmlspecialchars to prevent XSS, and validate that your SQL columns and values actually match the database (this is where found the fault). If you want a faster UX, populate the second dropdown and the results with AJAX so the page does not need multiple full submits.

Recommended Answers

All 19 Replies

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?

Member Avatar for Member #669958

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

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

Note quotations...

Member Avatar for Member #669958

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

Note quotations...

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

It should work, but anyhow, right after line 83, before </form>, add another hidden input field
<input type="hidden" name="act" value="view" />

On line 88, instead of $_GET, use:
$action = $_POST;

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

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";
Member Avatar for Member #669958

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:

<form name="form1" method="post" action="report.php?act=viewrep">

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.

Member Avatar for Member #669958

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

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.

First form's <option> 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 ["]].

For line 108:

echo "<a href='reportclient.php\?name=".$r."'>".$r."</a\>";
commented: he helped on solving this problem +0
Member Avatar for Member #669958

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.

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')
Member Avatar for Member #669958

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')

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.

commented: he gave some gd suggestions +1
Member Avatar for Member #669958

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.

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\">";
commented: it solved the problem +1
Member Avatar for Member #669958

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.