Jump to content

[SOLVED] Conditional List boxes - I am not sure why my form is not showing?


tlavelle

Recommended Posts

OK.  So I am attempting to create a list box that is dynamically populated based on the selection of another list box.  I am in way over my head..only way to learn I guess.  The code below has a year listbox and then based on the year selection, the page should either show another listbox (show industry selection when year <> 2005) or it should render the main form (when year=2005).  I cannot get the main form to appear.  Help please?

 

 

<?php
//initialize session
session_start();
?>

<html>
<head>
<basefont face="Arial">
</head>
<body>

<?php


// set server access variables
$host = "localhost";
$user = "root";
$pass = "";
$db = "wercbench";


// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create form 1 queries
$year_query = "SELECT distinct year FROM bench_data";



// execute form 1 queries
$year_result = mysql_query($year_query) or die ("Error in query: $query. ".mysql_error());
//$industry_result = mysql_query($industry_query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($year_result) > 0){
     // yes
     // print them one after another
if (!isset($_POST['wercbench1']) and !isset($_POST['year'])) {
 /*" name=\"wercbench1\">";*/
	echo"<form method=\"POST\" action=\"". $_SERVER['PHP_SELF']. "\" name=\"wercbench1\">";
	echo"<table style=\"width: 100%;\" border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
	echo"<tbody>";
	echo" <tr>";
	echo"   <td>Select the comparison year and group then click next";
	echo"   </td>";
	echo"   <td></td>";
	echo" </tr>";
	echo" <tr>";
	echo"  <td>";
	echo"   <select name=\"year\">";
		while($row = mysql_fetch_row($year_result)) {
			echo"<option value=\"" .$row[0]. "\">".$row[0]."</option>";
		}
	echo"   </select>";
	echo"   </td>";
	echo"   <td></td>";
	echo"	</tr><tr>";
	echo"   <td><input type=\"Submit\" value=\"Next\" name=\"wercbench1_next\"></button></td>";
	echo" </tr>";
	echo" </form>";
}
if(isset($_POST['wercbench1_next']) && !empty($_POST['year'])){
	if ($_POST['year']<>'2005'){
		$industry_result = mysql_query("SELECT distinct comp_desc FROM comp_desc") or die(mysql_error());

		echo" <tr>";
		echo"<form method=\"POST\" action=\"". $_SERVER['PHP_SELF']. "\" name=\"wercbench2_next\">";
			echo"   <td>";
			echo"   <select name=\"industry\">";
				while($row = mysql_fetch_row($industry_result)) {
					echo"<option value=\"" .$row[0]. "\">".$row[0]."</option>";
				}
				echo"   </select>";
				echo"</td>";
				echo"   <td></td>";
				echo" </tr>";
				echo" <tr>";
				echo"   <td><input type=\"Submit\" value=\"Next\" name=\"wercbench2_next\"></button></td>";
				echo"   <td></td>";
				echo" </tr>";

				echo"</tbody>";
				echo"</table>";
				echo"</form>";

        }
}
}

else {
    // no
    // print status message
    echo "No rows found!";


}



//Calcualtor variables
if(isset($_POST['wercbench2_next']))
{

$useryear=$_POST['year'];
$_SESSION['sess_year']=$_POST['year'];
$userindustry=$_POST['industry'];
$_SESSION['sess_ind']=$_POST['industry'];
$metric_query = "SELECT metric_desc.met_desc, bench_data.median, bench_data.best_pract FROM metric_desc, bench_data where metric_desc.met_key=bench_data.met_key and $useryear=bench_data.year";
$metric_result = mysql_query($metric_query) or die ("Error in query: $query. ".mysql_error());

if(!isset($_POST['wercbench2_next']) and (mysql_num_rows($metric_result)) > 0){

    // yes
    // print them one after another
//echo "<form action=\"results.php\" method=\"post\" name\"wercbench2\">";
echo "<form action=\"results.php\" method=\"post\" name\"wercbench2\">";
    echo "<table cellpadding=10 border=1>";
    echo "<tr>";
        echo "<td>Metric Description</td>";
        echo "<td>Average</td>";
        echo "<td>Best Practice</td>";
	echo "<td>Enter your value here</td>";
        echo "</tr>";
    while($row = mysql_fetch_row($metric_result)) {
        echo "<tr>";
        echo "<td>".$row[0]."</td>";
        echo "<td>" . $row[1]."</td>";
        echo "<td>".$row[2]."</td>";
	echo "<td><input type=\"text\" name=\"myvalue[]\" value=\"\" /></td>";
        echo "</tr>";
    }
    echo "<tr><td colspan=\"3\"> </td><td align=\"center\"><input type=\"submit\" name=\"wercbench_next2\" value=\"Compare\"></td></tr></table>";
}

// free result set memory
mysql_free_result($year_result);
mysql_free_result($industry_result);
mysql_free_result($metric_result);
//mysql_free_result($metric_result);
}
// close connection
mysql_close($connection);

?>

</body>
</html>

Link to comment
Share on other sites

what do you mean by the "main form"?  do you mean the original form with the year select box?  if so, you'll need to adjust the if() conditional on that block, or you could add an else{} statement to the if() conditional that displays the industry select box.

Link to comment
Share on other sites

Instead of saying the main form, I should say teh third form in the code.  Basically the logic is

 

1. Select a year

2.  If the year is 2007, select an industry

3.  IF the year is 2005 or the year is 2007 and an industry is selected, show form 3

 

it is step 3 above that is not working

Link to comment
Share on other sites

it sounds like this is the conditional you need for that third step:

 

if ((isset($_POST['wercbench1_next']) && $_POST['year'] == '2005') || isset($_POST['wercbench2_next']))

 

this will only show the last form if the first form was submitted with a 'year' value of 2005, or if the industry form was submitted.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.